New Android apps UniqueKey

Thursday, 28 August 2014

QUICK SORT


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<stdio.h>
#include<conio.h>

void quick ( int [ ] , int , int ) ;

main()
{
  int a[50] , first , last , i , j , n ;
 
  printf("enter the no. of element in array\n");
  scanf("%d",&n);
 
  printf("enter all elements\n");

   for ( i = 0 ; i < n ; i++ )
  scanf("%d",&a[i]);
  
  quick( a , 0 , n-1 ); 
  
  printf("sorted array\n");

   for ( i = 0 ; i < n ; i++ )
  printf("%d\t",a[i]);
  getch();
}

void quick ( int a[ ] , int first , int last )
{
  int i , j , pivot , temp ;

   if ( first < last )
  {
     pivot=first;
     i=first;
     j=last;
  
    while(i<j)
    {
      while(a[i]<=a[pivot]&&i<last)
      i++;
      while(a[j]>a[pivot])
      j--;
  
   if(i<j)
   {
     temp = a[i];
     a[i] = a[j];
     a[j] = temp;
   }
 }

  temp = a[pivot] ;
 a[pivot] = a[j] ;
 a[j] = temp ;

  quick ( a , first , j-1 ) ;
 quick( a , j+1 , last ) ;
  }
}
_________________________________________________________________________________

1 comment: