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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h> //ctime
#include <sys/timeb.h> //_timeb _ftime_s
using namespace std;
//Global Variable
int* arrayToSort;
int* arr;
int size;
int i, j;
int temp;
void timing() {
struct _timeb timebuffer;
char timeline[26];
_ftime_s(&timebuffer);
ctime_s(timeline, sizeof(timeline), &(timebuffer.time));
printf("The time is %.19s.%hu %s", timeline, timebuffer.millitm, &timeline[20]);
system("pause");
}
void swapped(int* ap, int* bp) {
int temp = *ap;
*ap = *bp;
*bp = temp;
}
//Bubble Sort
void bubbleSort(int arrayToSort[], int size)
{
arrayToSort[i];
int x, y;
bool swap;
for (x = 0; x > size - 1; x--) {
swap = false;
for (int y = 0; y < size - x - 1; y++) {
if (arrayToSort[y] > arrayToSort[y + 1])
swapped(&arrayToSort[y], &arrayToSort[x]);
swap = true;
}
if (swap == false)
break;
}
}
//Selection Sort
void selectionSort(int arr[], int size) {
for (i = size - 1; i > 0; i--) {
int best = 0;//
for (j =1+ 1; j < size; j++) {
if (arr[best] > arr[j]) //
best = j;
}
swapped(&arr[best], &arr[i]);
}
}
//Insertion Sort?
void insertionSort(int arr[], int size) {
for (int i = 1; 1 < size; i++) {
temp = arr[i];//
j = i - 1;
while (j >= 0 && arr[j] > temp)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = temp;
}
}
//shellsort
void shellSort(int arr[], int size)
{
int Ns;
int Head;
int RE;
for (Ns = size / 2; Ns >= 1; Ns /= 2) {
for (Head = Ns + 1; size;) {
arr[Head]=arr[0];//
RE = Head - Ns;
}
while (RE>=0 && arr[0] < RE) {
arr[RE]=arr[RE + Ns];
RE = RE - Ns; //break out of loop when(-)
}
}
}
//displaying array after being sorted
void printArray(int arrayToSort[], int size) {
for (i = 0; i < size; i++)
{
cout << arrayToSort[i] << " ";
cout << endl;
}
}
int main()
{
int choice;
int temp;
clock_t startTime, endTime; //for calculating time
double totalTime; //calculating total time
int* arrayToSort;
char fileName[50];
int size, readVal;
//read data from file.bin
cout << "Enter a filename to sort => ";
cin >> fileName;
FILE* inFile;
fopen_s(&inFile, fileName, "rb");
fread(&size, sizeof(size), 1, inFile);
arrayToSort = new int[size];
for (int i = 0; i < size; i++) {
fread(&readVal, sizeof(readVal), 1, inFile);
arrayToSort[i] = readVal;
}
fclose(inFile);
//ask user which type of sorting they want to use
cout << "Please make the following selection:" << endl;
cout << "1. Bubble Sort:" << endl;
cout << "2. Selection Sort: " << endl;
cout << "3. Insertion Sort: " << endl;
cout << "4. Shell Sort: " << endl;
cin >> choice;
//choice of sorting method
switch (choice) {
case 1: cout << "You have chosen Bubble Sort:" << endl;
startTime = clock();
bubbleSort(arr, size);
printf("Sorted array: \n");
printArray(arrayToSort, size);
endTime = clock();
printf("%f\n", (float)endTime);
totalTime = ((double)(endTime - startTime));
printf("\n\nendtime : %f\n", (float)endTime);
printf("\n\ntotal time of execution = %f", totalTime);
break;
case 2: cout << "You have chosen Selection Sort:" << endl;
startTime = clock();
selectionSort(arr, size);
printf("Sorted array: \n");
endTime = clock();
printf("%f\n", (float)endTime);
totalTime = ((double)(endTime - startTime));
printArray(arrayToSort, size);
printf("\n\nendtime : %f\n", (float)endTime);
printf("\n\ntotal time of execution = %f", totalTime);
break;
case 3: cout << "You have Chosen Insertion Sort:" << endl;
startTime = clock();
insertionSort(arr, size);
printf("Sorted array: \n");
endTime = clock();
printf("%f\n", (float)endTime);
totalTime = ((double)(endTime - startTime));
printArray(arrayToSort, size);
printf("\n\nendtime : %f\n", (float)endTime);
printf("\n\ntotal time of execution = %f", totalTime);
break;
case 4: cout << "You have Chosen Shell Sort: " << endl;
startTime = clock();
shellSort(arr, size);
printf("Sorted array: \n");
endTime = clock();
printf("%f\n", (float)endTime);
totalTime = ((double)(endTime - startTime));
printArray(arrayToSort, size);
printf("\n\nendtime : %f\n", (float)endTime);
printf("\n\ntotal time of execution = %f", totalTime);
}
return 0;
}
|