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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
#include <iostream>
using namespace std;
const int SIZE=20;
void bubbleSort(int numbers[], int SIZE);
void selectionSort(int numbers[], int SIZE);
void insertionSort(int numbers[], int SIZE);
int main()
{
int numbers[SIZE]= {26, 45, 56, 12, 78, 74, 39, 22, 5, 90, 87, 32, 28, 11, 93, 62, 79, 53, 22, 51};
int value=0;
bool found;
cout << "Today we are going to be searching for values." << endl;
cout << "These are the values you have to choose from" << endl;
for (int i=0; i<SIZE; i++)
cout << numbers[i]<<"; ";
do
{
cout << "Make sure to enter a value that's in the list." << endl;
cin >> value;
found=false;
for (int i=0; i<SIZE; i++)
{
if (value==numbers[i])
{
found=true;
break;
}
}
if (!found)
cout << "Enter a valid value !" << endl;
}
while (!found);
bubbleSort(numbers, SIZE);
selectionSort(numbers, SIZE);
insertionSort(numbers, SIZE);
return 0;
}
void bubbleSort (int numbers[], int SIZE)
{
cout<<"\nOriginal order: ";
for(int i=0;i<SIZE;i++)
cout<<numbers[i]<<' ';
int maxElement;
int index,counter=0;
for(maxElement=SIZE-1; maxElement>=0; maxElement--)
{
for(index=0;index<=maxElement-1;index++)
{
if(numbers[index]>numbers[index+1])
{
swap(numbers[index], numbers[index+1]);
counter++;//increments counter everytime swap occurs
}
}
}
cout<<"\nBubble Sorted: ";
for(int i=0;i<SIZE;i++)
cout<<numbers[i]<<' ';
cout<<"\nNumbers of location swap: "<<counter<<endl;
}
int swap(int &a, int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
void selectionSort(int numbers[], int SIZE)
{ cout<<"\nOriginal order: ";
for(int i=0;i<SIZE;i++)
cout<<numbers[i]<<' ';
int startScan;
int index;
int miniIndex;
int miniValue;
int counter=0;
for(startScan=0;startScan<(SIZE-1);startScan++)
{
miniIndex=startScan;
miniValue=numbers[startScan];
for(index=startScan+1;index<SIZE;index++)
{
if(numbers[index]<miniValue)
{
miniValue=numbers[index];
miniIndex=index;
}
}
swap(numbers[miniIndex], numbers[startScan]);
counter++;
}
cout<<"\nSelection Sorted: ";
for(int i=0;i<SIZE;i++)
cout<<numbers[i]<<' ';
cout<<"\nNumbers of location swap: "<<counter<<endl;
cout << endl;
}
void insertionSort(int numbers[], int SIZE)
{ cout<<"Original order: ";
for(int i=0;i<SIZE;i++)
cout<<numbers[i]<<' ';
int unsortedValue,counter=0;
int scan;
for(int index=1;index<SIZE;index++)
{
unsortedValue=numbers[index];
scan=index;
while(scan>0&&numbers[scan-1]>unsortedValue)
{
numbers[scan]=numbers[scan-1];
scan--;counter++;
}
numbers[scan]=unsortedValue;
}
void insertionSort (int arr[], int SIZE)
{
int j, temp, swap = 0;
cout<<"Original order: ";
for(int i = 0; i < SIZE; i++)
cout<< arr[i] << ' ';
for (int i = 0; i < SIZE; i++){
j = i;
while (j > 0 && arr[j] < arr[j-1]){
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
j--; swap++;
}
}
cout <<"\nThe number of location swaps is: "<< swap << endl;
return;
}
|