DS1(SORTING)(PART 4) || Quick Sort using C-Language - NESTED CODE || TECH FLOAT

Breaking

Post Top Ad

Post Top Ad

Friday, 24 August 2012

DS1(SORTING)(PART 4) || Quick Sort using C-Language


  4.  QUICK SORT

#include <stdio.h>
int split ( int*, int, int ) ;
void main( )
{
        int arr[100] ;
        int i,n;
    printf("Enter how many noumbers ??\n");
    scanf("%d",&n);
    printf("Put the numbers are in unsorted oriented\n");
    for(i=0;i<n;i++)
    {
                    scanf("%d",&arr[i]);
                    }
    printf("The Inputed array::\n");
    for(i=0;i<n;i++)
    {
                printf("%d ",arr[i]);
    }              
        void quicksort ( int *, int, int ) ;
    quicksort ( arr, 0, n ) ;
    printf("\n\nThe Sorted array is::\n");
        for ( i = 0 ; i < n ; i++ )
                        printf ( "%d ", 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 ;
}

Out put ::






No comments:

Post a Comment

Post Bottom Ad