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
|
# include <iostream>
using namespace std;
void bubbleSort (int arr[], int size)
{
bool swap;
int temp;
do
{
swap = false;
for (int count = 0; count < (size - 1) ; count ++)
{
if (arr[count] > arr [count + 1])
{
temp = arr [count];
arr [count] = arr [count + 1];
arr [count + 1] = temp;
swap = true;
}
}
} while (swap);
}
void selectionSort (int arr[], int size)
{
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = arr[startScan];
int index;
for (index = startScan + 1; index < size; index++)
{
if (arr[index] < minValue)
{
minValue = arr[index];
minIndex = index;
}
}
arr[minIndex] = arr[startScan];
arr[startScan] = minValue;
}
}
int main()
{
const int SIZE1 = 3;
const int SIZE2 = 8;
int arr [SIZE1][SIZE2] = { { 105, 102, 107, 103, 106, 100, 104, 101 },
{ 108, 106, 105, 110, 111, 100, 101, 107 },
{ 112, 118, 104, 103, 111, 100, 102, 101 }
};
for (int i = 0; i <= SIZE1 - 1; i++)
{
bubbleSort(arr[i], SIZE2);
cout << endl;
selectionSort(arr[i], SIZE2);
cout << endl;
}
system("pause");
return 0;
}
|