Tuesday, July 14, 2009

C++ beginner help plz?

const int size = 1000;


int sortarray[size];


int key = 0, j = 0;





int main(){





srand(time(0));


for(int i = 0; i %26lt; size; i++){ // starts the for loop


sortarray[i] = (rand()%10000) + 1; // makes numbers between 1 through 20 %/returns remainder


cout%26lt;%26lt;sortarray[i]%26lt;%26lt;" , ";


}





for(int i = 0; i %26lt; size; i++){ // starts the bubble sort


for(int j = size - 1; j %26gt; i; j--){ // starts the comparisons at the end of the array


if(sortarray [j] %26lt; sortarray[j - 1]){ // checks the current element with the previous one


key = sortarray[j]; // switches the elements if the conditions are met


sortarray[j] = sortarray[j - 1];


sortarray[j - 1] = key;


}


}


}





// formats the output prints to screen


cout%26lt;%26lt;endl%26lt;%26lt;endl;





for(int i = 0; i %26lt; size; i++){ // prints the sorted array





cout%26lt;%26lt;sortarray[i]%26lt;%26lt;", ";


}


return 0;


}


I wrote this code for bubble sort. How do I add a third array to fit the requirements of exchange sort method?

C++ beginner help plz?
Your code for bubble sort looks correct, but your question is confusing. Bubble sort is an exchange sort. Exchange sort is a general class of sorting algorithms, where the sort is executed by exchanging or swapping list elements.





I don't know what you mean by a third array (as you only have 1 array so far, the one to sort).
Reply:I suggest you to join C/C++ programming groups in yahoo for your programming doubts...


No comments:

Post a Comment