Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. /**
  4. *
  5. * @author Javargon
  6. *
  7. * www.javargon.com
  8. *
  9. * Problem Statement : Rewrite the Insertion-Sort procedure to sort
  10. * into non-increasing instead of non-decreasing order.
  11. * Exercise 2.1-2, Introduction to Algorithms, Cormen
  12. *
  13. */
  14. public class InsertionSortDecreasingDemo {
  15.  
  16. public static void main(String[] args) {
  17. int[] a = new int[]{31,41,59,26,41,58};
  18.  
  19. for(int i=1; i<a.length; i++) {
  20. int key = a[i];
  21. int j = i-1;
  22.  
  23. while(j >= 0 && a[j] < key) {
  24. a[j+1] = a[j];
  25. j--;
  26. }
  27. a[j+1] = key;
  28. }
  29.  
  30. System.out.println("Decreaing order -> "+Arrays.toString(a));
  31. }
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement