Zompies 20 Share Posted March 18, 2016 I have spent my previous day trying to solve this problem in my book. The problem was sort a array using the selection sort with recursion but to find the largest element and swap it with the end of the array. I spent like 4+ hours trying to solve it and it wouldn't work and I ended up in frustration and pissed off. It's like 5AM and I can't sleep because I kept getting the itch to try to solve it again and I finally said fuck it and I actually solved it. Here is the code. #include <stdio.h> void print_array(int, int[]); void selection_sort(int, int[], int); int main(void) { const int LEN = 8; int arr[] = {5,2,1,6,83,17,93,10}; int start = LEN; printf("Before sort\n"); print_array(LEN, arr); //sort selection_sort(LEN, arr, start); printf("After sort\n"); print_array(LEN, arr); return 0; } void print_array(int len, int arr[]){ for(int i = 0; i < len; i++){ printf("%d ", arr[i]); } printf("\n"); } void selection_sort(int len, int arr[], int start_pos){ /* * This sort starts at the end of the array and searches * for the highest element, it will find the index for the largest element * and swap that to the end of the array, what ever element is at end of array * goes where the largest element was */ int largest_index; //pos_of_unsorted = last element of array int pos_of_unsorted = start_pos - 1; largest_index = pos_of_unsorted; // assume its the start of our position // while we are above the first element, if we are -1 array is finished sorting // we then return to the caller if(pos_of_unsorted > 0){ for(int i = pos_of_unsorted; i > 0; i--){ if(arr[largest_index] < arr[i - 1]){ largest_index = i - 1; } } //swap elements //set temp to last element int temp = arr[pos_of_unsorted]; //2 3 1 //set last element to largest element arr[pos_of_unsorted] = arr[largest_index]; // 2 3 3 //set where we found largest element to the previous last value arr[largest_index] = temp; // 2 1 3 // pass in our length, our array, and our new position. selection_sort(len, arr, pos_of_unsorted); } } Link to comment Share on other sites More sharing options...
Nuclear Nezz 2056 Share Posted March 21, 2016 Please format your code so that it's readable Link to comment Share on other sites More sharing options...
Dogerina 330 Share Posted March 21, 2016 Please format your code so that it's readable stop using shit theme Link to comment Share on other sites More sharing options...
Zompies 20 Author Share Posted March 21, 2016 Please format your code so that it's readable I thought it is already formatted. Link to comment Share on other sites More sharing options...
Rabrg 83 Share Posted March 21, 2016 he's just talking about the bugged syntax highlighter for the dark theme, not ur code Link to comment Share on other sites More sharing options...
Nuclear Nezz 2056 Share Posted March 25, 2016 he's just talking about the bugged syntax highlighter for the dark theme, not ur code More the every line a different color thing. You could just give a pastebin link. That solves all the problems. :') Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.