IHYPRESS PROGRAMMING
Tutorials and C programs with code and output for beginners
c programming
HOME | ASP | C | CSS | GNUPLOT | HTML | JAVASCRIPT | PERL | PHP | PYTHON | RUBY | SVG
COne Dimensional Arrays : QuickSort
<07.13>
// sorting arrays using QuickSort (qsort) #include <stdio.h> #include <stdlib.h> #define SIZEW 9 #define SIZER 7 // for integers (ascending) int comp1 (const void * a, const void * b) { return ( *(int*)a - *(int*)b ); } // for doubles (descending) int comp2 (const void * a, const void * b) { return ( *(double*)b - *(double*)a ); } int main (void) { int i; int whole[] = {88, 56, 100, 2, 25, 13, 65, -34, 56}; double real[] = {13.4, -45.32, 123.1, 99.9, -10.1, 0.6, 25.0}; //sorting array of integers printf("Original array: "); for (i = 0; i < SIZEW; ++i) printf("%d ", whole[i]); qsort(whole, SIZEW, sizeof(int), comp1); printf("\nSorted array (^): "); for (i = 0; i < SIZEW; ++i) printf("%d ", whole[i]); //sorting array of doubles printf("\nOriginal array: "); for (i = 0; i <SIZER; ++i) printf("%.2lf ", real[i]); qsort(real, SIZER, sizeof(double), comp2); printf("\nSorted array (v): "); for (i = 0; i < SIZER; ++i) printf("%.2lf ", real[i]); return (0);
Hergestellt in Deutschland / Made in Germany
Original array: 88 56 100 2 25 13 65 -34 56 Sorted array (^): -34 2 13 25 56 56 65 88 100 Original array: 13.40 -45.32 123.10 99.90 -10.10 0.60 25.00 Sorted array (v): 123.10 99.90 25.00 13.40 0.60 -10.10 -45.32
COPYRIGHT © 2015-2024 IHY PRESS Frankfurt am Main 60329 Deutschland