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
|
#include <iostream>
using namespace std;
int BubbleSort(int bubble[], int size);
int SelectionSort(int selection[], int size);
int main(){
const int SIZE = 20;
int BubbleArray[SIZE] = {56,10,24,30,75,61,58,12,53,41,17,12,93,23,777,1,60,5,78,90};
int SelectionArray[SIZE] = {56,10,24,30,75,61,58,12,53,41,17,12,93,23,777,1,60,5,78,90};
cout << BubbleSort(BubbleArray, SIZE) << endl;
cout << SelectionSort(SelectionArray, SIZE) << endl;
return 0;
}
int BubbleSort(int bubble[], int size){
int temp;
bool swap;
int count = 0;
int HowMany = 0;
do{
swap = false;
for(count; count < (size - 1); count++){
if (bubble[count] > bubble[count + 1]){
temp = bubble[count];
bubble[count] = bubble[count + 1];
bubble[count + 1] = temp;
swap = true;
HowMany += 1;
}
}
}while(swap);
return HowMany;
}
int SelectionSort(int selection[], int size){
int startScan, minIndex, minValue;
int count = 0;
for(startScan = 0; startScan < (size - 1); startScan){
minIndex = startScan;
minValue = selection[startScan];
for(int index = startScan + 1; index < size; index++){
if(selection[index] < minValue){
minValue = selection[index];
minIndex = index;
count += 1;
}
}
selection[minIndex] = selection[startScan];
selection[startScan] = minValue;
}
return count;
}
|