InsertionSort

# InsertionSort - descendingInsertionSort(numbers1); //...

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

/* insetion sort pseudocode 1. a for loop to interate the array (i) 2. then assign a variable (x) = array at i, or each elements of the array 3. assign another variable (y) to compare the first variable with 4. interate a new for loop with (y), where (y) = i-1 5. if statement, array at y is less than x: do nothing/ break 6. else array at y+1 ( or next element) = y 7. come outside of for loop and assign array at y+1 = x */ public class InsertionSort { // the main method public static void main(String[] args) { // create the integer array first int[] numbers = {99, 2, 26, 23, 12, 14, 15, 76, 0, 50}; int[] numbers1 = {99, 2, 26, 23, 12, 14, 15, 76, 0, 50}; // display the values System.out.println("Array before sorting: "); display(numbers); // calls the display method // Sort the array in descending order using Insertion Sort algorithm insertionSort(numbers); // Sort the array in descending order using Insertion Sort algorithm

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: descendingInsertionSort(numbers1); // display the values again System.out.println("Array after sorting: "); display(numbers); // calls the display method display(numbers1); } public static void insertionSort(int a) { for (int i=1; i < a.length; i++) { int v = a[i]; int j; for (j = i - 1; j >= 0; j--) { if (a[j] <= v) break; a[j + 1] = a[j]; } a[j + 1] = v; } } public static void descendingInsertionSort(int a) { for (int i=1; i < a.length; i++) { int v = a[i]; int j; for (j = i - 1; j >= 0; j--) { if (a[j] >= v) break; a[j + 1] = a[j]; } a[j + 1] = v; } } // Method which displays all the values of the array in order static void display(int array) { for (int i = 0; i < array.length; i++) { if (i == array.length - 1) // last element System.out.println(array[i] + "\n"); else System.out.print(array[i] + " "); } } }...
View Full Document

## This note was uploaded on 02/06/2010 for the course CSE CIS3023 taught by Professor Seema during the Spring '10 term at Florida College.

### Page1 / 2

InsertionSort - descendingInsertionSort(numbers1); //...

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

View Full Document
Ask a homework question - tutors are online