Advertisement
Guest User

Untitled

a guest
Jan 25th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. /*
  2. BUBBLE SORT
  3. static memory allocation
  4. 1 based indexing
  5. ** for TURBO C, remove // from commented lines
  6. */
  7.  
  8. #include<stdio.h>
  9. //#include<conio.h>
  10.  
  11. #define swap(x,y) {temp = x; x = y; y = temp;}
  12.  
  13. void BUBBLE_SORT(int *arr, int arr_length){
  14. int i, j, temp;
  15. for(i = 1; i<= arr_length - 1; i++){
  16. for(j = arr_length; j >= (i+1); j--){
  17. if(arr[j] < arr[j-1]){
  18. swap(arr[j], arr[j-1]);
  19. }
  20. }
  21. }
  22. }
  23.  
  24. int main(){
  25. int i, len, arr[11];
  26. //clrscr();
  27. printf("\nEnter length (max 10): "); scanf("%d", &len);
  28. printf("\nEnter %d elements one after another:\n", len);
  29. for(i=1; i<=len; i++) scanf("%d", &arr[i]);
  30. printf("\nOriginal Array ::\n\t");
  31. for(i=1; i<=len; i++) printf("%d ", arr[i]);
  32. BUBBLE_SORT(arr, len);
  33. printf("\nSorted Array ::\n\t");
  34. for(i=1; i<=len; i++) printf("%d ", arr[i]);
  35. printf("\n");
  36. //getch();
  37. return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement