Posted By

jsmith on 01/10/12


Tagged

sort java algorithm sort-algorithm merge-sort


Versions (?)

Merge Sort


 / Published in: Java
 

  1. public static void main(String a[]){
  2. int i;
  3. int array[] = {12,9,4,99,120,1,3,10};
  4. System.out.println("\n\n RoseIndia\n\n");
  5. System.out.println(" Selection Sort\n\n");
  6. System.out.println("Values Before the sort:\n");
  7. for(i = 0; i < array.length; i++)
  8. System.out.print( array[i]+" ");
  9. System.out.println();
  10. mergeSort_srt(array,0, array.length-1);
  11. System.out.print("Values after the sort:\n");
  12. for(i = 0; i <array.length; i++)
  13. System.out.print(array[i]+" ");
  14. System.out.println();
  15. System.out.println("PAUSE");
  16. }
  17.  
  18. public static void mergeSort_srt(int array[],int lo, int n){
  19. int low = lo;
  20. int high = n;
  21. if (low >= high) {
  22. return;
  23. }
  24.  
  25. int middle = (low + high) / 2;
  26. mergeSort_srt(array, low, middle);
  27. mergeSort_srt(array, middle + 1, high);
  28. int end_low = middle;
  29. int start_high = middle + 1;
  30. while ((lo <= end_low) && (start_high <= high)) {
  31. if (array[low] < array[start_high]) {
  32. low++;
  33. } else {
  34. int Temp = array[start_high];
  35. for (int k = start_high- 1; k >= low; k--) {
  36. array[k+1] = array[k];
  37. }
  38. array[low] = Temp;
  39. low++;
  40. end_low++;
  41. start_high++;
  42. }
  43. }
  44. }
  45.  

Report this snippet  

You need to login to post a comment.