W.A.P. C++

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

Snehil Khanor's Binary Log
Showing posts with label sort. Show all posts
Showing posts with label sort. Show all posts

Write a program to sort elements of array using Selection Sorting


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. void main() 
  4. int a[100],n,i,j,temp,loc,min; 
  5. clrscr(); 
  6. printf("\How many elements:\n"); 
  7. scanf("%d",&n); 
  8. printf("Enter the element of array\n"); 
  9. for(i=0;i<=n-1;i++) 
  10. scanf("%d",&a[i]); 
  11. min=a[0]; 
  12. //Coding by: Snehil Khanor  
  13. //http://WapCPP.blogspot.com  
  14. for(i=0;i<=n-1;i++) 
  15. min=a[i]; 
  16. loc=i; 
  17. for(j=i+1;j<=n-1;j++) 
  18. if(a[j]<min) 
  19. min=a[j]; 
  20. loc=j; 
  21. if(loc!=1) 
  22. temp=a[i]; 
  23. a[i]=a[loc]; 
  24. a[loc]=temp; 
  25.  
  26. printf("The number after selection sorting are:\n"); 
  27. for(i=0;i<=n-1;i++) 
  28. printf("%d\n",a[i]); 
  29. getch(); 

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. }

Write a program to sort elements of array using Insert Sorting


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. main() 
  3. int a[100],n,k,i,j,temp; 
  4. printf("How many elements: \n"); 
  5. scanf("%d",&n); 
  6. printf("Enter the element of array: \n"); 
  7. for(i=0;i<=n-1;i++) 
  8. scanf("%d",&a[i]); 
  9. for(k=1;k<=n-1;k++) 
  10. temp=a[k]; 
  11. j=k-1; 
  12. //Coding by: Snehil Khanor 
  13. //http://WapCPP.blogspot.com 
  14. while((temp<a[j])&&(j>=0)) 
  15. a[j+1]=a[j]; 
  16. j=j-1; 
  17. a[j+1]=temp; 
  18. printf("Elements of array after sorting: \n"); 
  19. for(i=0;i<=n-1;i++) 
  20. printf("%d\n",a[i]); 
  21. getch();

Write a program to sort elements of array (bubble Sorting)


Size of array is taken from user.. Elemnts are entered by user.. Elements are sorted in ascending order..
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<conio.h>
  2. #include<iostream.h>
  3. void main()
  4. {
  5. clrscr();
  6. int a[100],i,n,j,temp;
  7. cout<<"How many element: ";
  8. cin>>n;
  9. cout<<"Enter the elements of array: "<<endl;
  10. for(i=0;i<n;i++)
  11. cin>>a[i];
  12. cout<<"The elements of array Before Sorting: "<<endl;
  13. //Coding by: Snehil Khanor
  14. //http://WapCPP.blogspot.com
  15. for(i=0;i<n;i++)
  16. cout<<a[i]<<" ";
  17. cout<<endl;
  18. for(i=0;i<n;i++)
  19. {
  20. for(j=0;j<n-1-i;j++)
  21. {
  22. if(a[j]>a[j+1])
  23. {
  24. temp=a[j];
  25. a[j]=a[j+1];
  26. a[j+1]=temp;
  27. }
  28. }
  29. }
  30. cout<<"Elements of array After Sorting: "<<endl;
  31. for(i=0;i<n;i++)
  32. cout<<a[i]<<" ";
  33. getch();
  34. }

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