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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
//In this lab, you will sort an array of constant integers in place using three different techniques.
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
void reset(const int array[], int num[], int size);
void displayIntArray(const int array[], int size);
void bubbleSort(int array[], int size);
void selectionSort(int array[], int size);
void insertionSort(int array[], int size);
int main()
{
const int size= 4;
int Oarray[size] = { 20, 40, 10, 30 };
int number[size];
cout << "Lab03" << endl;
cout << "The original array has bee reset to :" << endl;
displayIntArray(Oarray, size);
cout << endl;
cout << "SORTING - Bubble Sort Ascending - O(n2) algorithm" << endl;
bubbleSort(Oarray, size);
reset(Oarray, number, size);
cout << "SORTING - Selection Sort Ascending - O(n2) algorithm" << endl;
selectionSort(Oarray, size);
reset(Oarray, number, size);
cout << "SORTING - Insertion Sort Ascending - O(n2) algorithm" << endl;
insertionSort(Oarray, size);
cout << "DONE - Renato de Oyague" << endl;
system("pause");
return 0;
}
//1. reset function will be used to set the array to the original numbers
void reset(const int array[], int num[], int size)
{
cout << endl << "The original array has been reset to:" << endl;
for (int index = 0; index < size; index++)
{
num[index] = array[index];
}
for (int index = 0; index < size; index++)
{
cout << setw(9) << num[index];
}
cout << endl << endl;
}
//2. displayIntArray will be used to printout the content of the sorted array.
void displayIntArray(const int array[], int size)
{
for (int i = 0; i < size; i++)
{
cout << setw(9) << array[i];
}
cout << endl;
}
//3. Bubble sort function.
//The integers will be in ascending sorted order.
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;
displayIntArray(array, size); //Sort the content of the pointer array such that when you iterate through the array
//print out its content after every iteration,
}
}
}
while (swap);
cout << endl;
}
//4. You will repeat the same steps for Selection sort
void selectionSort(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;
displayIntArray(array, size);//Sort the content of the pointer array such that when you iterate through the array
//print out its content after every iteration,
}
cout << endl;
}
//5. You will repeat the same steps for Insertion sort.
void insertionSort(int array[], int size)
{
for (int i = 1; i < size; i++)
{
int j;
int current = array[i];
for (j = i - 1; j >= 0 && array[j] > current; j--)
{
array[j + 1] = array[j];
}
array[j + 1] = current;
displayIntArray(array, size);//Sort the content of the pointer array such that when you iterate through the array
//print out its content after every iteration,
}
}
|