Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sorts.java
- public class Sorts
- {
- public static void main (String[] args)
- {
- int[] grades = {89, 94, 69, 80, 97, 85, 73, 91, 77, 85, 93};
- DecendingSorts.selectionSort (grades);
- for (int grade : grades)
- System.out.print (grade + " ");
- }
- }
- and DecendingSorts.java
- public class DecendingSorts
- {
- //-----------------------------------------------------------------
- // Sorts the specified array of integers using the selection
- // sort algorithm.
- //-----------------------------------------------------------------
- public static void selectionSort (int[] numbers)
- {
- int min, temp;
- for (int index = 0; index < numbers.length-1; index++)
- {
- min = index;
- for (int scan = index+1; scan < numbers.length; scan++)
- if (numbers[scan] < numbers[min])
- min = scan;
- // Swap the values
- temp = numbers[min];
- numbers[min] = numbers[index];
- numbers[index] = temp;
- }
- }
- //-----------------------------------------------------------------
- // Sorts the specified array of integers using the insertion
- // sort algorithm.
- //-----------------------------------------------------------------
- public static void insertionSort (int[] numbers)
- {
- for (int index = 1; index < numbers.length; index++)
- {
- int key = numbers[index];
- int position = index;
- // shift larger values to the right
- while (position > 0 && numbers[position-1] > key)
- {
- numbers[position] = numbers[position-1];
- position--;
- }
- numbers[position] = key;
- }
- }
- //-----------------------------------------------------------------
- // Sorts the specified array of objects using the insertion
- // sort algorithm.
- //-----------------------------------------------------------------
- public static void insertionSort (Comparable[] objects)
- {
- for (int index = 1; index < objects.length; index++)
- {
- Comparable key = objects[index];
- int position = index;
- // shift larger values to the right
- while (position > 0 && objects[position-1].compareTo(key) > 0)
- {
- objects[position] = objects[position-1];
- position--;
- }
- objects[position] = key;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement