Hi I need java code for insertion sort selection sort and bu

Hi, I need java code for insertion sort, selection sort and bubble sort please.

Solution

Here is the code for Insertion Sort:

class InsertionSort
{
public static void insertionSort(double[] list)
{
for(int i = 1; i < list.length; i++)
{
double temp = list[i];
int j;
for(j = i - 1; j >= 0 && list[j] > temp; j--)
list[j+1] = list[j];
list[j+1] = temp;
}
}

public static void main(String[] args)
{
double list[] = {6.0, 4.4, 1.9, 2.9, 3.4, 2.9, 3.5};
  
System.out.println(\"The given array is: \");
for(int i = 0; i < list.length; i++)
System.out.print(list[i] + \" \");
System.out.println();
  
insertionSort(list);
  
System.out.println(\"The sorted array is: \");
for(int i = 0; i < list.length; i++)
System.out.print(list[i] + \" \");
System.out.println();
}
}

And the code for SelectionSort.java:

class SelectionSort
{
public static void selectionSort(double[] list)
{
for(int i = 0; i < list.length - 1; i++)
{
int max = 0;
for(int j = 0; j < list.length - i; j++)
if(list[j] > list[max])
max = j;
double temp = list[max];
list[max] = list[list.length - i - 1];
list[list.length - i - 1] = temp;
}
}

public static void main(String[] args)
{
double list[] = {6.0, 4.4, 1.9, 2.9, 3.4, 2.9, 3.5};
  
System.out.println(\"The given array is: \");
for(int i = 0; i < list.length; i++)
System.out.print(list[i] + \" \");
System.out.println();
  
selectionSort(list);
  
System.out.println(\"The sorted array is: \");
for(int i = 0; i < list.length; i++)
System.out.print(list[i] + \" \");
System.out.println();
}
}

And the code for BubbleSort.java is:

class BubbleSort
{
public static void bubbleSort(double[] list)
{
boolean changed = true;
do
{
changed = false;
for(int j = 0; j < list.length-1; j++)
if(list[j] > list[j+1])
{
double temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
changed = true;
}
}while(changed);
}

public static void main(String[] args)
{
double list[] = {6.0, 4.4, 1.9, 2.9, 3.4, 2.9, 3.5};
  
System.out.println(\"The given array is: \");
for(int i = 0; i < list.length; i++)
System.out.print(list[i] + \" \");
System.out.println();
  
bubbleSort(list);
  
System.out.println(\"The sorted array is: \");
for(int i = 0; i < list.length; i++)
System.out.print(list[i] + \" \");
System.out.println();
}
}

Hi, I need java code for insertion sort, selection sort and bubble sort please.SolutionHere is the code for Insertion Sort: class InsertionSort { public static
Hi, I need java code for insertion sort, selection sort and bubble sort please.SolutionHere is the code for Insertion Sort: class InsertionSort { public static

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site