Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Selection sort with recursion


    Zompies

    Recommended Posts

    Posted

    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);
    	}
    }
    
    
    Posted

    YA2bUhz.png

    Please format your code so that it's readable :(

     

    I thought it is already formatted.

    Posted

    he's just talking about the bugged syntax highlighter for the dark theme, not ur code

    Posted

    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. :')

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.