IHYPRESS.NET
C
HOME | ASP | C | CSS | HTML | JAVASCRIPT | PERL | PHP | PYTHON | RUBY | SVG
CAdvanced Functions ❯ Recursive Array Sort
<06.17>
#include <stdio.h> #define SIZE 11 //Recursive Selection Sort //Finds the largest value in list array[0]..array[n-1] and exchanges it //with the value at array[n-1] //Pre: n > 0 and first n elements of array are defined //Post: array[n-1] contains largest value void place_largest(int array[], //input/output - array in which to place largest int n) //input - number of array elements to consider { int temp, //temporary variable for exchange j, //array subscript and loop control max_index; //index of largest so far //Save subscript of largest array value in max_index max_index = n - 1; //assume last value is largest for (j = n - 2; j >= 0; --j) if (array[j] > array[max_index]) max_index = j; //Unless largest value is already in last element, // exchange largest and last elements if (max_index != n - 1) { temp = array[n - 1]; array[n - 1] = array[max_index]; array[max_index] = temp; } } // Sorts n elements of an array of integers // pre: n > 0 and first n elements of array are defined // post: array elements are in ascending order void select_sort(int array[], //input/output - array to sort int n) //input - number of array elements to sort { if (n > 1) { place_largest(array, n); select_sort(array, n - 1); } } int main (void) { int a[SIZE] = {67,33,89,32,65,129,13,87,54,90,22}; int i; select_sort (a,11); for (i = 0; i < SIZE; ++i) printf ("%d ", a[i]); return (0); }
Hergestellt in Deutschland / Made in Germany
13 22 32 33 54 65 67 87 89 90 129
COPYRIGHT © 2015-2024 IHYPRESS.NET. A DIVISION OF IHY PRESS. ALL RIGHTS RESERVED.