Program to sort given numbers using Merge Sort

c-techaravind
                                              To compile and execute the program, copy the code into a text file. Save the text file with the extinction .c (ex. program.c). Save your file in turboc2 or borland (it may your othe c-compiler). Now open you IDE (tc.exe) , goto file->pick file. Your source file will be loaded into IDE. Now you can compile and run the file easily.




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

int split ( int*, int, int ) ;

void main( )
{
int arr[10] ;
int i,n ;
gotoxy(31,1);
printf("Quick sort");
gotoxy(26,3);
printf("http:\\techaravind.blogsopt.com");
gotoxy(1,5);

printf("\nEnter the no. of elements for array:");
scanf("%d",&n);
printf("\nEnter the elements of the array:");
for(i=0;i<=n-1;i++)
{
scanf("%d",&arr[i]);
}
void quicksort ( int *, int, int ) ;

quicksort ( arr, 0, n-1 ) ;

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

for ( i = 0 ; i <= n-1 ; 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 ;
}

0 comments:

Post a Comment

 
Etutos © 2010-2011