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

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//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 <iostream>
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.