Sorts -...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
//******************************************************************** // Sorts.java Author: Lewis and Loftus // // Solution to Programming Project 6.12 // // Demonstrates the selection sort and insertion sort algorithms, // as well as a generic object sort. //******************************************************************** public class Sorts { //----------------------------------------------------------------- // 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.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: //-----------------------------------------------------------------public static void insertionSort (int numbers) { for (int index = 1; index &lt; numbers.length; index++) { int key = numbers[index]; int position = index; // shift larger values to the right while (position &gt; 0 &amp;&amp; numbers[position-1] &gt; 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 &lt; objects.length; index++) { Comparable key = objects[index]; int position = index; // shift larger values to the right while (position &gt; 0 &amp;&amp; objects[position-1].compareTo(key) &gt; 0) { objects[position] = objects[position-1]; position--; } objects[position] = key; } } }...
View Full Document

Page1 / 2

Sorts -...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online