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

Must use code provided for bubble sort and selection sorts but can be modified.

ID: 3806004 • Letter: M

Question

Must use code provided for bubble sort and selection sorts but can be modified.

public void selectionSort()

    {

        int loc, minIndex;

        for(loc = 0; loc < length; loc++)

        {

            minIndex = minLocation(loc, length - 1);

            swap(loc, minIndex);

        }

    }//end selectionSort

    private void swap(int first, int second)

    {

        DataElement temp;

        temp = list[first];

        list[first] = list[second];

        list[second] = temp;

    }//end swap

    private int minLocation(int first, int last)

    {

        int loc, minIndex;

        minIndex = first;

        for(loc = first + 1; loc <= last; loc++)

            if(list[loc].compareTo(list[minIndex]) < 0)

                minIndex = loc;

        return minIndex;

    }//end minLocation

public static void bubble_srt( int a[], int n ){

int i, j,t=0;

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

for(j = 1; j < (n-i); j++){

if(a[j-1] > a[j]){

t = a[j-1];

a[j-1]=a[j];

a[j]=t;

}

}

}

Create a new class named “SearchSort”. Your new class should do the following:

Instantiate a new array named myFavoriteMovies with 6 type String components

call method inputArray to fill the array with your favorite movies

call a method to print the contents of your list

use a sequential search to find a movie that you know is NOT in your list

use a sequential search to find a movie that you know IS in your list

use a bubble sort to sort your list in ascending order

call a method to print the contents of your list

Instantiate a new array named myFavoriteMusic with 5 type String components

call method inputArray to fill the array with your favorite music groups

call a method to print the contents of your list

use an selection sort to sort your list in descending order

call a method to print the contents of your list

use a binary search to find a group that you know is NOT in your list

use a binary search to find a group that you know IS in your list

Explanation / Answer

Java Program

import java.util.Scanner;

public class SearchSort {
   public static String[] selectionSort(String []aa)
   {
   int loc, minIndex;
     
   for(loc = 0; loc < aa.length; loc++)
   {
   minIndex = minLocation(loc, aa.length - 1,aa);
   swap(loc, minIndex,aa);
   }
   return aa;
   }//end selectionSort
     
   private static void swap(int first, int second,String []list)
   {
   String temp;
     
   temp = list[first];
   list[first] = list[second];
   list[second] = temp;
   }//end swap
     
   private static int minLocation(int first, int last,String[] list)
   {
   int loc, minIndex;
     
   minIndex = first;
     
   for(loc = first + 1; loc <= last; loc++)
   if(list[loc].compareTo(list[minIndex]) > 0)
   minIndex = loc;
     
   return minIndex;
   }//end minLocation
     
  
   public static String[] bubble_srt( String a[], int n ){
   int i, j;
   String temp;
   for(i = 0; i < n; i++){
   for(j = 1; j < (n-i); j++){
       if((a[j-1].compareToIgnoreCase(a[j]))>0){
           temp = a[j-1];
           a[j-1]=a[j];
           a[j]=temp;
   }
   }
  
   }
   return a;
   }
   public static void sequential_srch(String myfavourite[]){
       int c=0;
       System.out.println("enter name to search for using sequential search: " );
               Scanner sc = new Scanner(System.in);
               String search = sc.next();
       for (c = 0; c < myfavourite.length; c++)
       {
       if (myfavourite[c].equalsIgnoreCase(search)) /* Searching element is present */
       {
           System.out.println(search + " is present in list.");
       break;
       }
       }
       if (c == myfavourite.length)
           System.out.println(search + " is not present in array.");
       }
   public static int binarySearch(String[] words, String value, int min, int max) {
   if (min > max) {
   return -1;
   }
  
   int mid = (max + min) / 2;
  
   if (words[mid].equals(value)) {
   return mid;
   } else if(words[mid].compareTo(value) > 0) {
   return binarySearch(words, value, min, mid - 1);
   } else {
   return binarySearch(words, value, mid + 1, max);
   }
   }
   public static String inputArray(String type, int n){
       System.out.println("enter name of "+type+(n+1));
       Scanner sc = new Scanner(System.in);
       return sc.nextLine();
      
   }
   public static void printContent(String aa[]){
       System.out.println("Contents are:");
       for(String name:aa){
           System.out.println(name);
       }
   }
   public static void main(String[] args) {
       //Movies
       String []myfavouriteMoview = new String[6];
       for(int i=0;i<6;i++){
           myfavouriteMoview[i]=inputArray("movie",i);
       }
       printContent(myfavouriteMoview);
       sequential_srch(myfavouriteMoview);
       sequential_srch(myfavouriteMoview);
       String []bubbleSortedList = bubble_srt(myfavouriteMoview, myfavouriteMoview.length);
       System.out.println("List after bubble sort");
       printContent(bubbleSortedList);
      
       //Music
       String []myfavouriteMusic = new String[5];
       for(int i=0;i<5;i++){
           myfavouriteMusic[i]=inputArray("music",i);
       }
       printContent(myfavouriteMusic);
       String []selectionSort = selectionSort(myfavouriteMusic);
       System.out.println("List after selection sort in descending order");
       printContent(selectionSort);
      
       for(int i=0;i<2;i++){
           System.out.println("enter name to search for: " );
               Scanner sc = new Scanner(System.in);
               String search = sc.next();
       int loc = binarySearch(selectionSort,search , 0, selectionSort.length-1);
       if(loc!=-1)
       System.out.println("words present at location "+(loc+1));
       else
           System.out.println("word is not present in list");
       }
      

   }

}

Output:-

enter name of movie1
captain america
enter name of movie2
iron man
enter name of movie3
avengers
enter name of movie4
gravity
enter name of movie5
solar mass
enter name of movie6
zinc
Contents are:
captain america
iron man
avengers
gravity
solar mass
zinc
enter name to search for using sequential search:
zinc
zinc is present in list.
enter name to search for using sequential search:
transporter
transporter is not present in array.
List after bubble sort
Contents are:
avengers
captain america
gravity
iron man
solar mass
zinc
enter name of music1
shakira
enter name of music2
lean on
enter name of music3
will you
enter name of music4
i wanna
enter name of music5
justin
Contents are:
shakira
lean on
will you
i wanna
justin
List after selection sort in descending order
Contents are:
will you
shakira
lean on
justin
i wanna
enter name to search for:
sir
word is not present in list

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote