Sorting

hi,

I have been trying to sort the elements in ascending order. But I am kind of stuck at the switching part. Don't know how to continue from there.
This is my code:
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
#include <iostream>
#include <algorithm>
using namespace std;

// Declare a 9x9 array
int arraytable[9][9] = {{90,6,3,40,5,8,7,2,1},{16,2,3,4,80,7,1,50,9}};
int row1,row2;

 int main()
{
// Print the table
for (int row1 = 0; row1 < 9; row1++)
{
    for (int row2 = 0; row2 < 9; row2++)
	{
		if(arraytable[row2]>arraytable[row2+1]) // comparison between two adjacent array elements
		{
		 swap(arraytable[row2+1],arraytable[row2]);
		}
        cout << arraytable[row1][row2] << "\t";
	}
 cout << endl;
}

printf("\n\n");
system("pause");
}


really need some advice.
you know that
1
2
// Declare a 9x9 array
int arraytable[9][9] = {{90,6,3,40,5,8,7,2,1},{16,2,3,4,80,7,1,50,9}};

declares a 2x9 array, right?

the other 7 rows are unused


for your sorting problem: i have no idea what your swap function looks like. but just comparing two adjacent elements is not the proper way to do this.

run through all entries, detect the maximum and save it at the highest index. then run through all entries except the highest one, and detect and save the highest value in the 2nd highest index. and so on until the array is sorted.


or simply use std::sort
Last edited on
yea, the rest of the 7 rows are 0.

Is there another way to code it, if so how do i code it so the codes will not be lengthy but at the same time make use all of the rows??

for the sorting problem : the swap function that i try to use is not thus need some examples of

1) run through all entris [I can use a for loop to do that ]
2) detect the maximum [ not really sure , but it has something to with if - else ?]
3) save it at the highest index [how to go about doing it ]

need some guidance
for a 1 dimensional array


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
	int A[5] = {2,3,5,4,1};
	for(int i=4; i>=1; --i)
	{
		int index = i;
		int maximum = A[i];
		for(int j =i-1; j>=0; --j) //run from 4 to 0 since you look for the maximum. if you look for the minimum run from 0 to 4
		{                          //this way you make sure a once detected max/min doesnt get changed again.
			if(A[j] > maximum)     //
			{                      //find
				maximum = A[j];    //maximum
				index = j;         //and 
			}                      //index
		}
		int temp = A[i];	//swap maximum with the element with the highest index.
		A[i] = A[index];	//use temp, so A[i] doesnt get overwritten and lose its data this way
		A[index] = temp;	//
	}
}


short version
1
2
3
4
5
6
7
#include <algorithm>

int main()
{
	int A[5] = {2,3,5,4,1};
	std::sort(A, &A[5]);
}


if you dont know the size
1
2
3
4
5
6
7
#include <algorithm>

int main()
{
	int A[5] = {2,3,5,4,1};
	std::sort(A, A+sizeof(A)/sizeof(A[0])); //All A[i] have the same size, instead of A[0] you could also use A[1], A[2],...whatever
}
Last edited on
In C++11 you can use std::begin and std::end to get the start and end iterators to the array.
std::sort(std::begin(A), std::end(A));
In C++11 you can use std::begin and std::end to get the start and end iterators to the array.
std::sort(std::begin(A), std::end(A));


ah thats why my auto fill function didnt know std::begin and std::end

hmmm, correct me if I am wrong

1
2
3
4
5
6
7
8
9
10
int main()
{
	int A[5] = {2,3,5,4,1};
	for(int i=4; i>=1; --i)     
	{
		int index = i;     //  variable index now have the value of i
		int maximum = A[i];    //  this define the max value for this list? 
                
        }  


1
2
3
4
5
6
7
8
9
 //  I am confused with this set of for loop
for(int j =i-1; j>=0; --j)     
 {                         
	if(A[j] > maximum)    
		{                      
			maximum = A[j];   
			index = j;        
		}                     
}



1
2
3
4
// this code is use for swapping the elements positions in the list? 
int temp = A[i]; 	//swap maximum with the element with the highest index.
A[i] = A[index];	  //use temp, so A[i] doesn't get overwritten and lose its data this way
A[index] = temp;	//  


really confuse.
Last edited on
with A[5] = [2,3,5,4,1}; you have an array with 5 elements

the idea is to find the maximum of all 5 entries and save it to A[4]. but you can't just use A[4] = max, because then A[4] gets overwritten and the "1" is lost for ever. thats why i use temp to swap the entries.

so after this is done you have the maximum (=5) in A[4] and the 1 in A[2], which leaves you with 2,3,1,4,5.

the 5 was the found maximum and it has now the highest index, so you don't need to check it again, since it is at the correct spot. ("4" is also already correct here, but the pc doesnt know that yet)

in the next step you look for the 2nd highest number, which is the maxmium of the remaining numbers (all except the "5"). in the next step for the 3rd highest, and so on. thats why the first for loop starts at 4 (the highest index of A) and gets decreased.

for the combination of the first and the second for loop i give you another example.
we just reduce A to 3 elements compare A[2], A[1] and A[0]:
A[2] with A[1], A[2] with A[0]
A[1] with A[0]

or in other words
A[i=2] with A[i-1=1], A[i=2] with A[i-2=0];
A[i=1] with A[i-1=0]

if you define j=i-1, you get the 2 for loops
Last edited on
I think i really need to read up a bit more. Any website that will help explain this in a more simple way ??
i would use a debugger and look at it step by step.
think the sorting algorithm i used there is bubble sort, but i'm not sure about that. It was just the first thing that came to my mind.


if you have problems with the code it posted:

1) create 2 integer variables, set their values and then swap the values. so for example
int i1=1;
int i2=2;
and then change it in a way you get i1==2 and i2==1;

2) print (cout) all the elements of an array

3) print all possible combinations of 2 elements of that array. the order of the 2 elements doesnt matter, so element1-element2, is the same as element2-element1


you should understand what i did there, once you finish these tasks
Last edited on
but I dun understand this line .

1
2
3
4
int maximum =A[i];

cout<<maximum;



the result is -8589934601453

totally have no link to the value in i
Last edited on
i is always the highest index in each run and the element with the highest index saves the maximum after each run. so you set maximum = A[i] as starting value.
if there is a bigger number than A[i] it will get swapped with A[i]. if there is no bigger number, A[i] will get swapped with itself
so lets say A[5] = [2,3,5,4,1};


if i just stop at

int maximum =A[i];

cout<<maximum;

the elements in A[5] array swapped with itself??
if so how come the value is -8589934601453?? it does not make sense to me
please post the whole code, i dont know where you have problems and why
basically I was trying to understand ur code. so i rip off some of it.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

// Declare a 9x9 array
 int main()
{
	int A[5] = {2,3,5,4,1};
	for(int i=5; i>=1; --i)
	{
		int index = i;
		int maximum=A[i];
		cout<<index;
	}

printf("\n\n");
system("pause");
} 
what i did there is the following:

you have an array A[5] = {2,3,5,4,1};


i=4- save A[4] to maximum (=1)
i=4,j=3=i-1- check if A[3] > maximum: since this is true, maximum is now = A[3] (=4)
i=4,j=2- check if A[2] > maximum: sinc this is true, maximum is now = A[2] (=5)
i=4,j=1- check if A[1] > maximum: thats not true, so maximum is still 5
i=4,j=0- check if A[0] > maximum: thats not true, so maximum is still 5
i=4- swap maximum (= A[2]) with A[4]
=> now you have 2,3,1,4,5

i=3- save A[3] to maximum (=4)
i=3,j=2=i-1- check if A[2] > maximum: false, so do nothing
i=3,j=1- check if A[1] > maximum: false, so do nothing
i=3,j=0- check if A[0] > maximum: false, so do nothing
i=3- swap maximum (=A[3]) with A[3]
=> now you have 2,3,1,4,5

i=2- save A[2] to maximum (=1)
i=2,j=1=i-1- check if A[1] > maximum: thats the case, so maximum becomes A[1] (=3)
i=2,j=0- check if A[0] > maximum: thats not the case, so maximum is still A[1]
i=2- swap maximum (=A[1]) with A[2]
=> now you have 2,1,3,4,5

i=1- save A[1] to maximum (=1)
i=1,j=1=i-1- check if A[0] > maximum: since this is true, maximum = A[0] (=2)
i=1- swap maximum (=A[0]) with A[1]
=> now you have 1,2,3,4,5





Last edited on
wow ok, i think i kind of understand.
But there is still this small bit I don't understand.

1
2
3
int temp = A[i]; 	//swap maximum with the element with the highest index.
A[i] = A[index];	  //use temp, so A[i] doesn't get overwritten and lose its data this way
A[index] = temp;	//   


why A[i] = A[index]; ? swap the index in A with the values in A ?
and A[i] = A[index];?
Last edited on
Topic archived. No new replies allowed.