Hi all,
My next project in my self-teaching of C++ is sorting programs. I have chosen -- Selection sort -- as my teaching device.
I've chosen this code for a simple selection sort because I think it will serve me better than what I have.
I understand that the for loopCode:for(int x=0; x<n; x++) { int index_of_min = x; for(int y=x; y<n; y++) { if(array[index_of_min]>array[y]) { index_of_min = y; } } int index_of_min = x; for(int y=x; y<n; y++) { if(array[index_of_min]>array[y]) { index_of_min = y; } }can be both x < n, and x > n, thus going from big to small and from small to big. Thus, I can use the same sorting program to find numbers that have the highest frequency and the lowest.Code:for(int x=0; x<n; x++)
*int index_of_min = x* is a horse of a different color. I understand that it is an "int" and that x is equal to the index.Code:int index_of_min = x; for(int y=x; y<n; y++) { if(array[index_of_min]>array[y]) { index_of_min = y; } }
I also think it is a part of an int array. . . beyond that, I know it not. I don't know what it represents.
*for(int y=x; y<n; y++)* I understand that it is a standard nested for loop, and that y<n can also be y>n.
All I know about this is that there are two arrays-- [index_of_min] and [y]Code:if(array[index_of_min]>array[y])
beyond that I do not know how to use it.
I know as a simple assignment.Code:index_of_min = y;
Can someone please give me an example of how to use it? Let's say that I have a file that contains 5 int elements that needs sorting
(23, 75, 2, 9, 44) and let's say the file's name is "cow_one." How could I use this to sort this file?



LinkBack URL
About LinkBacks


