COP3530/Project14/Project14.cpp

54 lines
No EOL
1.4 KiB
C++

// Corey Williams
// COP3530 01Z
// Project 14
/*
Write an application in C++ or Java that uses the “Comb Sort” template described in
section 9.1.4, pp. 500-501 to write an application that will sort an array of 50 randomly generated integers.
The application must perform the following.
Display the unsorted numbers
Display the sorted numbers
*/
#include <iostream>
using namespace std;
template<class T>
void combsort(T data[], const int n) {
int step = n, j, k;
while ((step = int(step / 1.3)) > 1) // phase 1
for (j = n - 1; j >= step; j--) {
k = j - step;
if (data[j] < data[k])
swap(data[j], data[k]);
}
bool again = true;
for (int i = 0; i < n - 1 && again; i++) // phase 2
for (j = n - 1, again = false; j > i; --j)
if (data[j] < data[j - 1]) {
swap(data[j], data[j - 1]);
again = true;
}
}
int main() {
int a[50];
for (int i = 0; i <= 49; i++) { //step through the array loading with rand integers
a[i] = rand();
}
cout << "Unsorted Elements:\n"; //iterate through and print each element of the unsorted array
for (int i = 0; i <= 49; i++) {
cout << a[i] << endl;
}
combsort(a, 50);
cout << "\nSorted Elements:\n"; //iterate through and print each element of the sorted array
for (int i = 0; i <= 49; i++) {
cout << a[i] << endl;
}
}