W.A.P. C++

WAP | CPP | Programs in C++ | C++ Solutions

Snehil Khanor's Binary Log

Write a program to sort elements of array using Quick Sort


Size of array is taken from user.. Elements are entered by user in any order... Sorting is performed..

Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<stdio.h> 
  2. #include<conio.h> 
  3. #define max 100 
  4. int a[max],n,i,l,h; 
  5. void main() 
  6. void input(void); 
  7. input(); 
  8. getch(); 
  9.  
  10. void input(void) 
  11. void output(int a[],int n); 
  12. void quick_sort(int a[],int l,int h); 
  13. printf("How many elements in the array : "); 
  14. scanf("%d",&n); 
  15. printf("\n"); 
  16. printf("Enter the elemennts : \n"); 
  17. for(i=0;i<=n-1;i++) 
  18. scanf("%d",&a[i]); 
  19. l=0; 
  20. h=n-1; 
  21. quick_sort(a,l,h); 
  22. printf("Sorted Array :\n "); 
  23. output(a,n); 
  24.  
  25. void quick_sort(int a[],int l, int h) 
  26. int temp,key,low,high; 
  27. low=l; 
  28. high=h; 
  29. key=a[(low+high)/2]; 
  30. do 
  31. while(key>a[low]) 
  32. low++; 
  33. while(key<a[high]) 
  34. high--; 
  35. if(low<=high) 
  36. temp=a[low]; 
  37. a[low++]=a[high]; 
  38. a[high--]=temp; 
  39. //Coding by: Snehil Khanor 
  40. //http://WapCPP.blogspot.com 
  41. } while(low<=high); 
  42. if(l<high) 
  43. quick_sort(a,l,high); 
  44. if(low<h) 
  45. quick_sort(a,low,h); 
  46. void output(int a[],int n) 
  47. for(i=0;i<=n-1;i++) 
  48. printf("%d\n",a[i]); 
  49. }

1 comments:

Anonymous February 16, 2014 at 9:59 PM  

guys.....there is complete explanation i have seen for quick sort

Algorithm and Implementation Program of Quick Sort
http://geeksprogrammings.blogspot.com/2014/02/algorithm-quick-sort-program.html

Search

About WAP C++

Here you'll find a wide range of programs' solution ranging from beginer level to advanced level.
All programs here are made, compiled and tested by me..and are running absolutely fine.. still if you find any bug in any program do let me know :)

Followers

Subscribe via email

Enter your email address:

Delivered by FeedBurner

my Binary Log



eXTReMe Tracker