I'm writing my own sorting algorithm and needed some help. I want to write it in C++ but I'm not to sure how. Here is and example how I want it to work.
Step 1: 56712983 -> comparisons are made between first element and last then 2nd element to 2nd to last and so on.
Step 2: 36712985 -> now the elements are divided by two and both sides are compared like the step 1
3671 | 2985
Step 3: 1673 | 2895 -> now i want to divide both sides by two again and sort
16 | 73 | 28 | 95
Step 4: 16372859 -> now the elements are back together and I want to do the first step again.
Step 5: 15327869 -> now I finish off the sorting process with a bubble sort.
*note: If the array is an odd amount of elements I want the middle element to drop down. Also in step 3 I want to divide down to 2 or 3 elements depending on if its odd or even.
I know this sorting algorithm isn't really that great but I wanted to try and write my own so if I could get any ideas that'd be cool.. if not thats cool to!..haha
below is my bubble sort that I wrote in C++ which is my last step ..but i'm still not sure how to write the code to compare the first and last element as I showed in my steps then divide.
Code:const int size = 1000; // this is the array size int sortarray[size]; // this is the array which I'll use later int key = 0, j = 0; int main(){ srand(time(0)); // this part starts the random number generator for(int i = 0; i < size; i++){ // starts the for loop sortarray[i] = (rand()%1000) + 1; // makes numbers between 1 through 20 %/returns remainder cout<<sortarray[i]<<", "; } for(int i = 0; i < size; i++){ // starts the bubble sort for(int j = size - 1; j > i; j--){ // starts the comparisons at the end of the array if(sortarray [j] < sortarray[j - 1]){ // checks the current element with the previous one key = sortarray[j]; // switches the elements if the conditions are met sortarray[j] = sortarray[j - 1]; sortarray[j - 1] = key; } } } // formats the output prints to screen cout<<endl<<endl; for(int i = 0; i < size; i++){ // prints the sorted array cout<<sortarray[i]<<", "; } return 0; }