-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection_Sort_yt.c
More file actions
64 lines (58 loc) · 2.21 KB
/
Selection_Sort_yt.c
File metadata and controls
64 lines (58 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*******************************************************************************
*
* Program: Selection Sort
*
* Description: Example of selection sort algorithm implemented in C. See the
* wikipedia article: https://en.wikipedia.org/wiki/Selection_sort
*
* YouTube Lesson: https://www.youtube.com/watch?v=YepJ7fDmyjI
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <stdio.h>
// An example of applying each step of the selection sort algorithm...
//
// Initial array: 5, 9, 7, 6, 4, 0, 2, 3, 8, 1
//
// Step 0: 0, 9, 7, 6, 4, 5, 2, 3, 8, 1
// Step 1: 0, 1, 7, 6, 4, 5, 2, 3, 8, 9
// Step 2: 0, 1, 2, 6, 4, 5, 7, 3, 8, 9
// Step 3: 0, 1, 2, 3, 4, 5, 7, 6, 8, 9
// Step 4: 0, 1, 2, 3, 4, 5, 7, 6, 8, 9
// Step 5: 0, 1, 2, 3, 4, 5, 7, 6, 8, 9
// Step 6: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
// Step 7: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
// Step 8: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
int main()
{
// a test array and array length
int a[] = {5, 9, 7, 6, 4, 0, 2, 3, 8, 1};
int length = 10;
// Loop through each index of the array, as we do so the elements < i are
// considered the sorted portion of the array, and the elements >= i are
// considered the unsorted portion of the array. The algorithm repeatedly
// finds the minimum element in the unsorted portion of the array and if
// necessary swaps it with the element at index i, increasing the portion
// of the sorted array with each iteration.
for (int i = 0; i < length - 1; i++)
{
// find the position of the minimum element in the unsorted portion of
// the array
int min_pos = i;
for (int j = i + 1; j < length; j++)
if (a[j] > a[min_pos]) min_pos = j;
// if that element is NOT the element at index i, then swap that element
// with the element at index i
if (min_pos != i)
{
int temp = a[i];
a[i] = a[min_pos];
a[min_pos] = temp;
}
}
// print out the array so we can be sure it is sorted correctly
for (int i = 0; i < length; i++)
printf("a[%d] = %d\n", i, a[i]);
return 0;
}