English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I was also wondering if you could write a pseudo code to explain the algorihthm

public class InsertionSorter
{

public static int[ ] sort(int[ ] list)// passes an array list to sort
{
for (int i = 1; i < list.length; i++)// outer loop goin through list starts at 1 coz we need to check index 1 value
{
int value = list[i]; // local variable initialised holds value
int j = i-1;// get previous index
while (j >= 0 && list[j] > value)// inner loop condition 2 check if greater than/= 0 and greater than previous number
{
list[j + 1] = list[j]; // move until j is less than value
j = j-1; // next number
}
list[j+1] = value; // set value
}
return list;
}
}

2007-12-13 04:23:37 · 3 answers · asked by sprite 3 in Computers & Internet Programming & Design

3 answers

To test the code, feed it a list of random integers, a list that is sorted in reverse order, and a list that contains all the same value. If the algorithm sorts the lists without crashing or throwing exceptions, I'd say it's working correctly.

Insertion sort begins by considering the first element in the array to be a sorted sublist of length one. The algorithm then takes the next element and inserts it into the sorted sublist until it is at the correct position. Repeat until done.

Below is how a six is inserted into a sorted sublist of length four.

1 4 8 12 6 11 19
1 4 8 6 12 11 19
1 4 6 8 12 11 19

The algorithm now has a sorted sublist of length five and will next insert the eleven.

2007-12-13 04:32:43 · answer #1 · answered by jgoulden 7 · 0 0

1

2017-01-19 16:34:00 · answer #2 · answered by Anonymous · 0 0

confusing issue. browse from search engines like google. it can assist!

2014-11-06 16:12:38 · answer #3 · answered by Anonymous · 0 0

fedest.com, questions and answers