Created
December 26, 2019 03:05
-
-
Save AliYmn/55fdae40e146460b49218696227b6cbe to your computer and use it in GitHub Desktop.
Selection Sort Basic Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i want to write note for to the future. Programming 1 Final prepare :(