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
|
//Ashton Dreiling
//Sorting Benchmarks exercise
#include <iostream>
#include <stdlib.h>
using namespace std;
//module prototypes
int binarySearch(int t, int arr[], int n);
int bubbleSort(int num[], int n);
void printArray(int arr[], int SIZE);
//global constants
const int SIZE=20;
const int NUM_FOR_CAL1=1;
const int NUM_FOR_CAL2=2;
const int ZERO_FOR_CAL=0;
int main()
{
//some variables
int swap;
int arr[SIZE] = {26, 45, 56, 12, 78, 74, 39, 22, 5, 90, 87, 32, 28, 11, 93, 62, 79, 53, 22, 51};
//Showing original order, bubble sort, and the number of swaps
cout << "Original Order : ";
printArray(arr, SIZE);
swap = bubbleSort(arr, SIZE);
cout << "Bubble Sorted : ";
printArray(arr, SIZE);
cout << "Number of location swaps: " << swap << endl;
int num, pos, total = ZERO_FOR_CAL;
char YesNo;
do
{
cout << "Select a number in the Array to search for: ";
cin >> num;
pos = binarySearch(num, arr, SIZE);
cout << "Sequential Search comparisons: " << pos + NUM_FOR_CAL1<< endl;
cout << "The position of the number is " << pos + NUM_FOR_CAL1 << endl;
if(pos != -NUM_FOR_CAL1) total++;
cout << "Binary Search comparisons: " << total << endl;
cout << "The position of the number is " << pos + NUM_FOR_CAL1 << endl;
cout << "Do you want to search again (Y = Yes) : ";
YesNo = NUM_FOR_CAL2;
cin >> YesNo;
}//end of do while loop to search for array and display comparisons
while(YesNo == 'Y' || YesNo == 'y');
system("Pause");
return 0;
}//end main
//searching array using binarySearch
int binarySearch(int t, int arr[], int n)
{
for(int i = NUM_FOR_CAL2; i < n; ++i)
if(arr[i] == t) return i;
return -NUM_FOR_CAL1;
}//end of binarySearch
//searching array using bubbleSort
int bubbleSort(int num[], int n)
{
int i, j, flag = NUM_FOR_CAL1;
int temp, swap = NUM_FOR_CAL2;
for(i = NUM_FOR_CAL1; (i <= n) && flag; i++)
{
flag = NUM_FOR_CAL2;
for (j = NUM_FOR_CAL2; j < (n-NUM_FOR_CAL1); j++)
{
if (num[j+NUM_FOR_CAL1] < num[j])
{
temp = num[j];
num[j] = num[j+NUM_FOR_CAL1];
num[j+NUM_FOR_CAL1] = temp;
flag = NUM_FOR_CAL1;
swap++;
}//end of if statement
}//end of for loop
}//end of for loop
return swap;
}//end bubbleSort
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;
}
cout<<"\nInsertion Sorted: ";
for(int i=0;i<SIZE;i++)
cout<<numbers[i]<<' ';
cout <<"\nThe number of location swaps is: "<<counter << endl;
}
void printArray(int arr[], int SIZE)
{
for(int i = 0; i < SIZE; i++)
{
cout << arr[i];
if(i < SIZE - 1) cout << ", ";
}
cout << endl;
}
|