### printing after each pass, sorting

Okay, so my program works fine, but I am trying to add something in here and I just don't know how to add it in.

For each sort, I need the program to print out the array contents after each pass of each sort. I know it's just a small loop and I have an idea of where to put it...but its the construction of the loops that has me lost.

Any help is appreciated

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107`` ``````//This program will look at two identical arrays and then the two seperate functions //will sort the numbers into ascending order; printing out the array after each pass //and finally print out the array after it has been ordered #include using namespace std; //~~~~~~~~~~~~~~~~~~~ //Bubble Sort int bubbleSort(int array1[]) { // Print out the array how it is first declared cout << "BubbleSort" << endl << "*********************************" << endl; cout << ("Before sorting:"); for (int i = 0; i < 8; i++) { cout << array1[i]; } // These loops will sort out the array bool order = false; while (!order) { order = true; for (int i=0; i < 8 ; i++) { if (array1[i] > array1[i+1]) { int temp = array1[i]; array1[i] = array1[i+1]; array1[i+1] = temp; order = false; //&&&&&&&&&&&&&&&&&&&&&&&&&&&THE LOOP FOR PRINTING THE ARRAY WOULD GO HERE? } //end if }//end for }//end while // print the sorted array cout << ("\nAfter sorting:"); for (int i = 0; i < 8; i++) { cout << array1[i]; } cout << endl; }//end bubblesort //~~~~~~~~~~~~~~~~~~~ //Selection sort int selectionSort(int array2[]) { // Print out the array how it is first declared cout << "Selection Sort" << endl << "*********************************" << endl; cout << ("Before sorting:"); for (int i = 0; i < 8; i++) { cout << array2[i]; } for(int x=0; x<8; x++) { int index_of_min = x; for(int y=x; y<8; y++) { if(array2[index_of_min]>array2[y]) { index_of_min = y; } } int temp = array2[x]; array2[x] = array2[index_of_min]; array2[index_of_min] = temp; //&&&&&&&&&&&&&&&&&&&&&&&&&&&THE LOOP FOR PRINTING THE ARRAY WOULD GO HERE? } // print the sorted array cout << ("\nAfter sorting:"); for (int i = 0; i < 8; i++) { cout << array2[i]; } cout << endl; }//end selectionSort //~~~~~~~~~~~~~~~~~~~ int main(){ int count = 0, array1[8], array2[8]; array1[0] = 8; array1[1] = 2; array1[2] = 4; array1[3] = 5; array1[4] = 9; array1[5] = 1; array1[6] = 3; array1[7] = 7; array2[0] = 8; array2[1] = 2; array2[2] = 4; array2[3] = 5; array2[4] = 9; array2[5] = 1; array2[6] = 3; array2[7] = 7; bubbleSort(array1); cout << endl << endl << endl; selectionSort(array2); system("PAUSE"); return 0; }//end main ``````
Last edited on
Topic archived. No new replies allowed.