C Programming Tutorial

 
 
 
 
#include <stdio.h>
#include <conio.h>

int split ( int*, int, int ) ;

void main( )
{
    int arr[10] = { 11, 2, 9, 13, 57, 25, 17, 1, 90, 3 } ;
    int i ;

    void quicksort ( int *, int, int ) ;

    clrscr( ) ;

    printf ( \"Quick sort.\\n\" ) ;
    printf ( \"\\nArray before sorting:\\n\") ;

    for ( i = 0 ; i <= 9 ; i++ )
        printf ( \"%d\\t\", arr[i] ) ;

    quicksort ( arr, 0, 9 ) ;

    printf ( \"\\nArray after sorting:\\n\") ;

    for ( i = 0 ; i <= 9 ; i++ )
        printf ( \"%d\\t\", arr[i] ) ;

    getch( ) ;
}

void quicksort ( int a[ ], int lower, int upper )
{
    int i ;
    if ( upper > lower )
    {
        i = split ( a, lower, upper ) ;
        quicksort ( a, lower, i - 1 ) ;
        quicksort ( a, i + 1, upper ) ;
    }
}

int split ( int a[ ], int lower, int upper )
{
    int i, p, q, t ;

    p = lower + 1 ;
    q = upper ;
    i = a[lower] ;

    while ( q >= p )
    {
        while ( a[p] < i )
            p++ ;

        while ( a[q] > i )
            q-- ;

        if ( q > p )
        {
            t = a[p] ;
            a[p] = a[q] ;
            a[q] = t ;
        }
    }

    t = a[lower] ;
    a[lower] = a[q] ;
    a[q] = t ;

    return q ;
}

    Related Post:
  1. PROGRAM TO CONVERT BINARY FLOTING POINT TO HEXADECIMAL FLOTING POINT

  2. Program for Number Conversion from Decimal to Binary, Decimal to Octal, Decimal to Hexa, Binary to Octal, Binary to Hexa, Octal to Hexa

  3. Example 3 of using function

  4. Reading and writing of alphabets in reverse case

  5. Program to take input text from a user. Also take 2 words one that need to replace and one that will be replaced from sentence

  6. PROGRAM TO CALCULATE BONUS, COMMISSION AND GROSS SALARY OF A SALESMAN

  7. PROGRAM TO READ NUMBERS, N1 AND N2 ARE INTERCHANGE WITHOUT USING EXTRA VARIABLES.N1=10, N2=20 THEN O/P IS N1=20, N2=10 USING MULTIPLICATION, DIVISION

  8. Program to find average of 5 subjects

  9. Program to express a length given in milimeters in meter and centimeters

  10. Program to display series 2,4,16,256... using while loop

  11. Program to find matrix addition, subtraction, multiplication, transpose and symmetric operations

  12. Program to generate triangle

  13. Program of FSEEK

  14. Program to exchange numbers using pointers

  15. PROGRAM OF CREAT HEADER FILES FOR P_FORK,P_JOIN AND SHARED MEMORY

  16. Program to create processes using fork() and check different states for orphan process

  17. Program to read person details and display records whose weight is greater than 60 using structure

  18. Program to assign a character variables to the pointer and to display the contents of the pointer

  19. String functions

  20. Program to calculate roots of 3 numbers using root1 = (-b + sqrt(discriminant))/(2.0*a) and root2 = (-b - sqrt(discriminant))/(2.0*a) formula

 
 
Didn't find what you were looking for? Find more on Quick sort