Bubble Sort Descending Order


SUBMITTED BY: Guest

DATE: Oct. 25, 2013, 9:31 p.m.

FORMAT: Java

SIZE: 824 Bytes

HITS: 1011

  1. //Bubble Sort for Descending Order
  2. public static void BubbleSort( int [ ] num )
  3. {
  4. int j;
  5. boolean flag = true; // set flag to true to begin first pass
  6. int temp; //holding variable
  7. while ( flag )
  8. {
  9. flag= false; //set flag to false awaiting a possible swap
  10. for( j=0; j < num.length -1; j++ )
  11. {
  12. if ( num[ j ] < num[j+1] ) // change to > for ascending sort
  13. {
  14. temp = num[ j ]; //swap elements
  15. num[ j ] = num[ j+1 ];
  16. num[ j+1 ] = temp;
  17. flag = true; //shows a swap occurred
  18. }
  19. }
  20. }
  21. }

comments powered by Disqus