Skip to content

Instantly share code, notes, and snippets.

@AliYmn
Created December 26, 2019 03:05
Show Gist options
  • Save AliYmn/55fdae40e146460b49218696227b6cbe to your computer and use it in GitHub Desktop.
Save AliYmn/55fdae40e146460b49218696227b6cbe to your computer and use it in GitHub Desktop.
Selection Sort Basic Example
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define N 10
void selection_sort(int arr[], int size);
void swap(int arr[], int firstIndex, int secondIndex);
int main()
{
int my_arr[N];
srand(time(NULL));
for (int i = 0; i < N; i++)
{
my_arr[i] = rand() % 99 + 11;
}
for (int k = 0; k < N; k++)
{
printf("%d\t", my_arr[k]);
}
selection_sort(my_arr, N);
printf("\n");
for (int k = 0; k < N; k++)
{
printf("%d\t", my_arr[k]);
}
return 0;
}
void selection_sort(int arr[], int size)
{
int i, j, minIndex;
for (i = 0; i < size; i++)
{
minIndex = i;
for (int j = i; j < size; j++)
{
if (arr[j] < arr[minIndex])
minIndex = j;
swap(arr, i, minIndex);
}
}
}
void swap(int arr[], int firstIndex, int secondIndex)
{
int temp;
temp = arr[firstIndex];
arr[firstIndex] = arr[secondIndex];
arr[secondIndex] = temp;
}
@AliYmn
Copy link
Author

AliYmn commented Dec 26, 2019

i want to write note for to the future. Programming 1 Final prepare :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment