Question: This is Java. Please let me know easy and simple code. 9.7 Insertion Sort Insertion sort is a sorting algorithm that treats the input as

This is Java. Please let me know easy and simple code.

9.7 Insertion Sort

Insertion sort is a sorting algorithm that treats the input as two parts, a sorted part and an unsorted part, and repeatedly inserts the next value from the unsorted part into the correct location in the sorted part.

As in selection sort, the sorted part of the array is initially of size one and is the leftmost element and the unsorted part is the rest of the array. Insertion sort walks through the elements of the array from left to right and determines where to 'insert' the elements into the sorted part of the array on the left.

A detailed example of how insertion sort works can be found here.

public class Sorting { public static void main(String[] args) { // Example input. int numbers[] = {10, 2, 78, 4, 45, 32, 7, 11}; int i;

int numbersSize = numbers.length; // Print array pre sorting. System.out.print("UNSORTED: "); for (i = 0; i < numbersSize; i++) { System.out.print(numbers[i] + " "); } System.out.println();

// Run Insertion Sort. insertionSort(numbers);

// Print array post sorting. System.out.print("SORTED: "); for (i = 0; i < numbersSize; i++) { System.out.print(numbers[i] + " "); } System.out.println(); }

public static void insertionSort(int numbers[]) { // FIXME } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!