First trick: crank up your compiler warnings and take care of them, one by one, until things compile correctly. (You have been advised this before.)
I do not know what you are using to edit your code, but find yourself a code editor that understands C++ with syntax highlighting and automatic indenting.
I have cleaned it up for you:
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
|
#include <algorithm> // for std::swap, if you aren't going to write your own swap function
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
// sorting using bubblesort
void bubbleSort(int bubbleArray[], int numb)
{
int temp;
for(int pass=0; pass<numb; pass++)
for(int i=0; i<numb-1; i++)
{
if(bubbleArray[i] > bubbleArray[i+1])
{
temp = bubbleArray[i];
bubbleArray[i]= bubbleArray[i+1];
bubbleArray[i+1]= temp;
}
}
return;
}
void selectionSort(int selectionArray[], int n)
{
int min;
for(int k = 0; k < n - 1; k++)
{
min = k;
for(int j = k+1; j < n; j++)
{
if(selectionArray[j] < selectionArray[min])
{
min = j;
}
}
swap(selectionArray[min], selectionArray[k]);
}
}
void printArray(int array[], int size)
{
for (int i = 0; i < size; i++)
cout << array[i] << " ";
}
int main()
{
constexpr int n=10;
int i, j, temp;
int bubbleArray [n];
int selectionArray [n];
int insertionArray [n];
int quickArray [n];
int mergeArray [n];
srand ((unsigned)time(NULL));
cout << "Random list of bubbleArray: ";
for (i = 0; i < 10; i++)
{
int value = rand() % 100; // Random from 1 through 100 ; allows for all arrays to generate the same random numbers
bubbleArray [i] = value;
selectionArray [i] = value;
insertionArray [i] = value;
quickArray [i] = value;
mergeArray [i] = value;
cout << bubbleArray [i] << " ";
}
cout << "\nRandom list of selectionArray: ";
printArray(selectionArray,n);
cout << "\nRandom list of insertionArray: ";
printArray(insertionArray,n);
cout << "\nRandom list of quickArray: ";
printArray(quickArray,n);
cout << "\nRandom list of mergeArray: ";
printArray(mergeArray,n);
bubbleSort(bubbleArray, i);
cout << endl << "\nThis is the list after being sorted by the bubble sort method:\n";
printArray(bubbleArray,n);
selectionSort(selectionArray, n);
cout << endl << "\nThis is the list after being sorted by the selection sort method:\n";
printArray(selectionArray,n);
// system("pause");
return 0;
}
|
Insertion sort is almost as simple as selection sort.
Quicksort and mergesort are basic sorts, but they are tricky to implement correctly.
For quicksort, the hardest part is the partion function.
Make life easy for yourself and just choose the first element in a section as the pivot. Remember to use swap() to move elements towards the ends.
For mergesort, there are options.
I recommend a bottom-up mergesort for ease. Then you only need to loop over the array by twos, then fours, then eights, etc, until you run out of partitions. Additionally, you can allocate the entire block of additional memory one time, at the toplevel invocation, and just reuse it with all your calls to the merge function.
Good luck!
[edit]
I take it back on the mergesort. Do a top-down sort. It keeps life easiest. The basic algorithm:
merge_sort( array, first, last ):
if (last - first < 2) return
mid = (last - first) / 2 + first
merge_sort( array, first, mid )
merge_sort( array, mid, last )
merge( array, first, mid, last )
The merge() function there is where you need to spend time. In my own implementation I added another argument to the functions for the additional space you need to do the merge. My main merge_sort function looks like:
1 2 3 4 5 6
|
void merge_sort( int array[], int n )
{
int* temp = new int[n/2+1];
merge_sort( array, 0, n, temp );
delete[] temp;
}
|
Good luck. Seriously.