Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the code for three sort algorithms ( Bubble sort, Selection sort, Insertio

ID: 3733871 • Letter: W

Question

Write the code for three sort algorithms ( Bubble sort, Selection sort, Insertion sort) that we talked about. Sort an array of Integers, strings

and an array of Cars using each technique. At least seven items in each case.

This is what I have done so far.

my main.

package lab6;

public class Main {

public static void main(String[] args) {

// TODO Auto-generated method stub

Integer[] list = new Integer[7];

list[0] = 3;

list[1] = 5;

list[2] = 1;

list[3] = 8;

list[4] = 7;

list[5] = 12;

list[6] = 20;

SortArry.selectionSort(list, 7);

for(int i=0; i<7; i++)

System.out.println(list[i]);

String[] list2 = new String[7];

list2[0] = "David";

list2[1] = "John";

list2[2] = "Wu";

list2[3] = "Hassan";

list2[4] = "Bob";

list2[5] = "MIke";

list2[6] = "Sam";

SortArry.selectionSort(list2, 7);

for(int i=0; i<7; i++)

System.out.println(list2[i]);

}

}

................................................................................................................................

Class SortArray

package lab6;

public class SortArry {

public static <T extends Comparable> void selectionSort(T[] a, int n)

{

for(int index = 0; index < n - 1; index++)

{

int indexOfNextSmallest = getIndexOfSmallest(a, index, n-1);

swap(a, index, indexOfNextSmallest);

}

}

private static <T extends Comparable> int getIndexOfSmallest(T[] a, int first, int last)

{

T min = a[first];

int indexOfMin = first;

for(int index = first + 1; index <= last; index++)

{

if (a[index].compareTo(min) < 0)

{

min = a[index];

indexOfMin = index;

}

}

return indexOfMin;

}

private static <T> void swap(T[] a, int i, int j)

{

T temp = a[i];

a[i] = a[j];

a[j] = temp;

}

}

Explanation / Answer

/* Insertion Sort on Array of Integers*/

class InsertionSort

{

    /*Function to sort array using insertion sort*/

    void sort(int arr[])

    {

        int n = arr.length;

        for (int i=1; i<n; ++i)

        {

            int key = arr[i];

            int j = i-1;

            /* Move elements of arr[0..i-1], that are

               greater than key, to one position ahead

               of their current position */

            while (j>=0 && arr[j] > key)

            {

                arr[j+1] = arr[j];

                j = j-1;

            }

            arr[j+1] = key;

        }

    }

    /* A utility function to print array of size n*/

    static void printArray(int arr[])

    {

        int n = arr.length;

        for (int i=0; i<n; ++i)

            System.out.print(arr[i] + " ");

        System.out.println();

    }

    // Driver method

    public static void main(String args[])

    {       

        int arr[] = {12, 11, 13, 5, 6};

        InsertionSort ob = new InsertionSort();       

        ob.sort(arr);

         

        printArray(arr);

    }

}

/*Bubble Sort on String */

import java.util.*;

public class BubbleSort {

public static void main(String[] args) {

String list[]={"Artichoke" , "Apple" , "Cherry" , "Banana","Guava","Mango","Pineapple"};

BubbleSort(list);

for(int i=0; i<list.length; i++)

{

System.out.println(list[i]);

}

}// End of Main

private static void BubbleSort(String[] array) {

String temp;

// Begin For loop

for(int i=0; i<array.length; i++) {

//Open For loop

for(int j=0; j<array.length-1-i; j++) {

//Open If Statement Compare and sort strings

if(array[j].compareTo(array[j+1])>0) {

temp = array[j];

array[j] = array[j+1];

array[j+1] = temp;

}//End of If Statement

}//End of For Loop

}//End of For Loop

}//End of Private Class BubbleSort

}// End of Public class Bub

/*Sorting Cars using Selection Sort*/