Question

---Using Java to solve--- Write the code for three sort algorithms. Sort an array of Integers,...

---Using Java to solve---

Write the code for three sort algorithms. Sort an array of Integers, strings and an array of Cars using each technique. At least seven items in each case.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* 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*/

import java.util.Scanner;
class program1
{
    public static void main(String[] args) 
    {
        int n;
        String temp;
        String names[] = {"Dodge" , "Porsche" , "Bugatti" , "Lamborghini","Chevrolet","Ferrari","Shelby"};
        for (int i = 0; i < names.length; i++) 
        {
            for (int j = i + 1; j < names.length; j++) 
            {
                if (names[i].compareTo(names[j])>0) /*comapareTo method is used to compare between the two names in the list */
                {
                    temp = names[i];
                    names[i] = names[j];
                    names[j] = temp;
                }
            }
        }
        for (int i = 0; i < names.length - 1; i++) 
        {
            System.out.print(names[i] + ",");
        }
        System.out.print(names[names.length - 1]);
    }
}
Add a comment
Know the answer?
Add Answer to:
---Using Java to solve--- Write the code for three sort algorithms. Sort an array of Integers,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT