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.
Code:
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;
}
}
I understand that the for loop
Code:
for(int x=0; x<n; x++)
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:
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* is a horse of a different color. I understand that it is an "int" and that x is equal to the index.
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.
Code:
if(array[index_of_min]>array[y])
All I know about this is that there are two arrays-- [index_of_min] and [y]
beyond that I do not know how to use it.
I know as a simple assignment.
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?