
how many arrays needed for insertion sort and why?

There should be a single one dimensional array. Here is an example of Insertion Sort. It sorts the integer array.
class InsertSort{
public static void insertionSort(int a[]){
int in, out;
for(out=1; out<a.length; out++)
{
int temp = a[out];
in = out;
while(in>0 && a[in-1] >= temp)
{
a[in] = a[in-1];
--in;
}
a[in] = temp;
}
}
public static void main(String[] args)
{
int arr[]={99,55,33,44,88,11};
insertionSort(arr);
for(int j=0; j<arr.length; j++)
System.out.print(arr[j] + " ");
System.out.println("");
}
}
You can also sort string array. Here is an example of sorting string array.
public class InsertionSort {
public static void sort(String[] array) {
int N = array.length;
for (int i = 1; i < N; i++)
for (int j = i; j > 0; j--)
if (array[j-1].compareTo(array[j]) > 0)
change(array, j, j-1);
else break;
}
private static void change(Comparable[] a, int i, int j) {
Comparable swap = a[i];
a[i] = a[j];
a[j] = swap;
}
public static void main(String[] args) {
String[] array ={"S","D", "A","B","Z", "M","O", "L","H", "Y"};
sort(array);
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
}
For more information, visit the following link:
http://www.roseindia.net/java/beginners/arrayexamples/InsertionSort.shtml
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.