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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
// Thomas Kim
// 2/19/14
#include <iostream>
#include <iomanip>
using namespace std;
// Prototypes
void reset (const int startNum[], int num [], int size);
void displayIntArray (const int num[], int size);
void BubbleSort(int num [], int size);
void SelectionSort(int num [], int size);
void insertionSort(int num [], int size);
int main ()
{
const int SIZE = 4;
const int start_num [SIZE] = {20, 40, 10, 30};
int numbers [SIZE];
cout << "Lab03 \n";
reset (start_num, numbers, SIZE);
cout <<"\nSORTING - Bubble Sort Ascending - O(n2) algorithm\n";
BubbleSort (numbers, SIZE);
reset ( start_num, numbers, SIZE);
cout<<"\n\nSORTING - Selection Sort Asccending - O(n2) algorithm\n";
SelectionSort (numbers, SIZE);
reset ( start_num, numbers, SIZE);
cout<<"\n\nSORTING - Insertion Sort Acending - O(n2) algorithm\n";
insertionSort (numbers, SIZE);
cout<<"\n\nDONE -Thomas Kim\n";
cout <<endl <<endl;
system("pause");
return 0;
}
//
void reset (const int startNum[], int num[], int size)
{
cout << endl << "The original array has been reset to:" << endl;
for (int index = 0; index < size; index++)
{
num[index] = startNum[index];
}
for (int index = 0; index < size; index++)
{
cout << setw(5) << num[index];
}
cout << endl;
}
//
void displayIntArray (const int num[], int size)
{
for (int index = 0; index < size; index++)
{
cout << setw(5) << num[index];
}
}
//
void BubbleSort(int num [], int size)
{
bool swap;
int temp;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (num[count] > num [count + 1])
{
temp = num[count];
num[count] = num[count +1];
num[count +1] = temp;
swap = true;
}
}
displayIntArray (num, size);
cout << endl;
} while (swap);
}
//
void SelectionSort(int num [], int size)
{
int startscan, minindex, minvalue;
for (startscan = 0; startscan < (size - 1); startscan++)
{
minindex = startscan;
minvalue = num[startscan];
for(int index = startscan + 1; index < size; index++)
{
if (num[index] < minvalue)
{
minvalue = num[index];
minindex = index;
}
}
num[minindex] = num[startscan];
num[startscan] = minvalue;
displayIntArray (num, size);
cout << endl;
}
}
//
void insertionSort(int num [], int size)
{
for (int i = 1; i < size; i++)
{
int j;
int current = num[i];
for (j = i - 1; j >= 0 && num[j] > current; j--)
{
num[j+1]=num[j];
}
num[j+1]=current;
displayIntArray (num, size);
cout << endl;
}
}
|