This time the video is about selection sort. I’m fine with arrays and linked lists so far(I think I’m goin to use it in the future) and with selection sort, too, just bubble sort and selection sort are so similar. By the way, I need to say that I’m enjoying this book really much and until now, it has been my favorite book about anything that has to do with knowledge. It seems like I get to know more things from nowhere while reading a comic book. The pictures are nice!
Algorithms are fun, too, but philosophy is also fun with “An outline of philosophy” by Russell! I have a new “Brilliant idea” to make a much cooler siries of video… About philosophy! I’ll like to share what I had read with other people, and trust me… This book isn’t a comic book, but just as much fun! Russell has been satirizing people, not using one rude word. And the chapters all have meaningful titles that will “grab” you right into it!
1# include <stdio.h>
2# include <stdlib.h>
3# include <limits.h>
4
5# define SIZE 6
6
7int find_smallest(int *array)
8{
9 int smallest = array[0];
10 int smallest_index = 0;
11 for (int i = 1; i < SIZE; i++)
12 {
13 if (array[i] < smallest)
14 {
15 smallest = array[i];
16 smallest_index = i;
17 }
18 }
19 return smallest_index;
20}
21
22int find_largest(int *array)
23{
24 int largest = array[0];
25 int largest_index = 0;
26 for (int i = 1; i < SIZE; ++i)
27 {
28 if (array[i] > largest)
29 {
30 largest = array[i];
31 largest_index = i;
32 }
33 }
34 return largest_index;
35}
36
37int *selection_sort(int *array)
38{
39 int *newArray = (int *)malloc(SIZE * sizeof(int));
40 for (int i = 0; i < SIZE; i++)
41 {
42 //int smallest = find_smallest(array);
43 int largest = find_largest(array);
44 //newArray[i] = array[smallest];
45 newArray[i] = array[largest];
46 //array[smallest] = INT_MAX;
47 array[largest] = INT_MIN;
48 }
49 return newArray;
50}
51
52int main(void)
53{
54 int array[SIZE] = {9, 18, 4, 7, 2, 20};
55 int *sarray = selection_sort(array);
56 for (int i = 0; i < SIZE; i++)
57 {
58 printf("%d\n", sarray[i]);
59 }
60 return 0;
61}