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
|
#include<iostream>
using namespace std;
void bubbleSort(int [], int);
void bubbleShow(const int[], int);
void selectSort(int [], int);
void selectShow(const int[], int);
int main()
{
const int SIZE = 20;
int values[SIZE] = {20,1,19,2,18,3,17,4,16,5,15,6,14,7,13,8,12,9,10,11};
int values2[SIZE] = {20,1,19,2,18,3,17,4,16,5,15,6,14,7,13,8,12,9,10,11};
cout<<"The unsorted values of the Bubble sort array are\n";
bubbleShow(values, SIZE);
bubbleSort(values, SIZE);
cout<<"The sorted values of the Bubble sort array are\n";
bubbleShow(values, SIZE);
cout<<"The unsorted values of the Selection sort array are\n";
selectShow(values2, SIZE);
selectSort(values2, SIZE);
cout<<"The sorted values of the Selection sort array are\n";
selectShow(values2, SIZE);
system("pause");
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
void bubbleSort(int array[], int size)
{
bool swap;
int temp;
do
{
swap=false;
for(int count =0; count < (size -1); count++)
{
if (array[count] > array[count +1])
{
temp = array[count];
array[count] = array[count +1];
array[count+1] = temp;
swap = true;
}
}
}
while (swap);
}
/////////////////////////////////////////////////////////////////////////////////////////////
void bubbleShow(const int array[], int size)
{
for(int count = 0; count < size; count++)
cout<<array[count]<<" ";
cout<<endl;
}
/////////////////////////////////////////////////////////////////////////////////////////////
void selectSort(int array[], int size)
{
int startScan, minIndex, minValue;
for (startScan =0; startScan< (size -1); startScan++)
{
minIndex = startScan;
minValue = array[startScan];
for(int index = startScan +1; index <size; index++)
{
if (array[index] <minValue)
{
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
void selectShow(const int array[], int size)
{
for (int count = 0; count < size; count++)
cout<< array[count] <<" ";
cout<<endl;
}
|