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
|
//includers
#include <iostream>
#include <iomanip>
//using name space
using namespace std;
void selectionSort(int A[], int ISIZE);
void sort(int B[], int ISIZE);
int main()
{
//variable in arrays
const int ISIZE = 7;
//Array A Selection sort
int A[ISIZE] = { 88,75,71,31,55,98,89 };
cout << "Selction Sort Functon" << endl;
selectionSort(A, ISIZE);
//Array B bubble sort
int B[ISIZE] = { 88,75,71,31,55,98,89 };
cout << "Bubble Sort Function" << endl;
sort(B, ISIZE);
//print
system("pause");
return 0;
}
void selectionSort(int A[], int ISIZE) //Ascending
{
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (ISIZE - 1); startScan++)
{
minIndex = startScan;
minValue = A[startScan];
for (int index = startScan + 1; index < ISIZE; index++)
{
if (A[index] < minValue)
{
minValue = A[index];
minIndex = index;
}//if
for (int i = 0; i < ISIZE; i++) {
cout << A[i] << ' ';
}
cout << endl;
}//for index
A[minIndex] = A[startScan];
A[startScan] = minValue;
}//for start scan
}//selectionsort
void sort(int B[], int ISIZE)
{
int temp;
bool swap;
do
{
swap = false;
for (int count = 0; count < (ISIZE - 1); count++)
{
if (B[count] > B[count + 1])
{
temp = B[count + 1];
B[count + 1] = B[count];
B[count] = temp;
swap = true;
}//if
}//forcount
} while (swap);
}//bubble sort
|