samroar04

Untitled

Feb 11th, 2014
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void insertsort(int a[], int n) {
  4. int i,j,target;
  5. for(i = 1 ; i < n ; i++) {
  6. target=a[i];
  7. j=i-1;
  8. while((j >= 0) && (target <= a[j])) {
  9. a[j+1]=a[j];
  10. j=j-1;
  11. }
  12. a[j+1]=target;
  13. }
  14. }
  15. void display(int a[], int n) {
  16. int i;
  17. for(i = 0 ; i < n ; i++) {
  18. printf("%d\n",a[i]);
  19. }
  20. }
  21. void main() {
  22. int a[20],n,i;
  23. clrscr();
  24. printf("Enter the total number of elements");
  25. scanf("%d",&n);
  26. printf("Enterthe elements\n");
  27. for(i = 0 ; i < n ; i++) {
  28. scanf("%d",&a[i]);
  29. }
  30. insertsort(a,n);
  31. printf("\n Array elements after sorting\n");
  32. display(a,n);
  33. getch();
  34. }
  35.  
  36.  
  37. #include<stdio.h>
  38. int main() {
  39. int s,i,j,temp,a[20];
  40. clrscr();
  41. printf("Enter total elements: ");
  42. scanf("%d",&s);
  43. printf("Enter %d elements: ",s);
  44. for(i = 0 ; i < s ; i++) {
  45. scanf("%d",&a[i]);
  46. }
  47. for(i = 0 ; i < s ; i++) {
  48. for(j = i+1 ; j < s ; j++) {
  49. if(a[i]>a[j]) {
  50. temp=a[i];
  51. a[i]=a[j];
  52. a[j]=temp;
  53. }
  54. }
  55. }
  56. printf("After sorting is: ");
  57. for(i = 0 ; i < s ; i++) {
  58. printf(" %d",a[i]);
  59. }
  60. getch();
  61. }
Advertisement
Add Comment
Please, Sign In to add comment