Java Programming Project Help! (Insertion Sorting) Assignment Details; Add searc
ID: 3809697 • Letter: J
Question
Java Programming Project Help! (Insertion Sorting)
Assignment Details;
Add searching.java, sorting.java and Numbers.java to your project.
Work on the Numbers.java and fill the blanks with proper code. You need to add the code to invoke the searching method to search a target number. Make sure use the binary search. Print out the searching result properly.
Run the project. Provide the initial integer list as 1, 4, 3, 2, 5. Try searching number 2 first and other different numbers, in the list and not in the list. Is the result correct?
The reason is that the list must be sorted before running binary search. (Need to be sorted before running linear search?) Add code to invoke the insertion sort method before searching.
Now the searching result should be correct.
Let’s search a list of a different type. Use the Contact class provided. Since it has already implemented the Comparable interface, you do not need to change it.
Create a driver class. In the main method, declare one array of Contact type with size 5. Instantiate the array with your own data.
Call the insertion sorting method in the provided Sorting class to sort the array. Use a for loop to print out the sorting result.
Then call the binary searching algorithm to search a particular Contact object. Again, try searching elements that is in the list and not in the list.
Here are all of the provided codes;
public class searching {
public static Comparable linearSearch (Comparable[] list,
Comparable target)
{
int index = 0;
boolean found = false;
while (!found && index < list.length)
{
if (list[index].equals(target))
found = true;
else
index++;
}
if (found)
return list[index];
else
return null;
}
public static Comparable binarySearch (Comparable[] list,
Comparable target)
{
int min=0, max=list.length, mid=0;
boolean found = false;
while (!found && min <= max)
{
mid = (min+max) / 2;
if (list[mid].equals(target))
found = true;
else
if (target.compareTo(list[mid]) < 0)
max = mid-1;
else
min = mid+1;
}
if (found)
return list[mid];
else
return null;
}
}
-------------------------------------------------------------------------------------------------------------
//********************************************************************
// Sorting.java Author: Lewis/Loftus
//
// Solution to Programming Project 10.5
//********************************************************************
public class sorting
{
//-----------------------------------------------------------------
// Sorts the specified array of objects using the selection
// sort algorithm.
//-----------------------------------------------------------------
public static void selectionSort (Comparable[] list)
{
int min;
Comparable temp;
for (int index = 0; index < list.length-1; index++)
{
min = index;
for (int scan = index+1; scan < list.length; scan++)
if (list[scan].compareTo(list[min]) < 0)
min = scan;
// Swap the values
temp = list[min];
list[min] = list[index];
list[index] = temp;
}
}
//-----------------------------------------------------------------
// Sorts the specified array of objects using the insertion
// sort algorithm.
//-----------------------------------------------------------------
public static void insertionSort (Comparable[] list)
{
for (int index = 1; index < list.length; index++)
{
Comparable key = list[index];
int position = index;
// Shift larger values to the right
while (position > 0 && key.compareTo(list[position-1]) < 0)
{
list[position] = list[position-1];
position--;
}
list[position] = key;
}
}
}
-------------------------------------------------------------------------------------------------------------
//**********************************************************
// Numbers.java
//
// Demonstrates selectionSort on an array of integers.
//**********************************************************
import java.util.Scanner;
public class Numbers
{
//---------------------------------------------
// Reads in an array of integers, sorts them,
// then prints them in sorted order.
//---------------------------------------------
public static void main (String[] args)
{
Integer[] intList;
int size;
int target;
Scanner scan = new Scanner(System.in);
System.out.print (" How many integers do you want to sort? ");
size = scan.nextInt();
intList = new int[size];
System.out.println (" Enter the numbers...");
for (int i = 0; i < size; i++)
intList[i] = scan.nextInt();
System.out.print (" Input the integer you want to search: ");
target = scan.nextInt();
//Your code here
//Invoke the binary searching algorithm to search for target
//Your code here
//Output the searching result, if found, print the integer; if not found, print out a message
}
}
-------------------------------------------------------------------------------------------------------------
Explanation / Answer
HI, PLease find my implementation of required code:
//**********************************************************
//Numbers.java
//
//Demonstrates selectionSort on an array of integers.
//**********************************************************
import java.util.Scanner;
public class Numbers
{
//---------------------------------------------
// Reads in an array of integers, sorts them,
// then prints them in sorted order.
//---------------------------------------------
public static void main (String[] args)
{
Integer[] intList;
int size;
int target;
Scanner scan = new Scanner(System.in);
System.out.print (" How many integers do you want to sort? ");
size = scan.nextInt();
intList = new Integer[size];
System.out.println (" Enter the numbers...");
for (int i = 0; i < size; i++)
intList[i] = scan.nextInt();
System.out.print (" Input the integer you want to search: ");
target = scan.nextInt();
//Your code here
//Invoke the binary searching algorithm to search for target
Integer index = (Integer) searching.binarySearch(intList, target);
//Your code here
//Output the searching result, if found, print the integer; if not found, print out a message
if(index != null)
System.out.println(target+ " is present");
else
System.out.println(target+" is not available in list");
}
}
/*
Sample run:
How many integers do you want to sort? 5
Enter the numbers...
5
1
2
3
7
Input the integer you want to search: 4
4 is not available in list
*/
######## ContactDriver.java ##########
import java.util.Scanner;
public class ContactDriver {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Contact[] contacts = new Contact[5];
contacts[0] = new Contact("Pravesh", "Kumar", "1234567");
contacts[1] = new Contact("ALex", "E", "65345");
contacts[2] = new Contact("Bob", "Xender", "987564");
contacts[3] = new Contact("Mukesh", "Rahul", "3221345");
contacts[4] = new Contact("Lokesh", "M", "098347");
System.out.println("Before Sorting: ");
for(Contact c : contacts)
System.out.println(c);
// calling insertion sort
sorting.insertionSort(contacts);
//displaying
System.out.println(" After Sorting: ");
for(Contact c : contacts)
System.out.println(c);
System.out.println(" Enter contact details to be sarch : ");
System.out.print("Enter first name: ");
String firstName = scan.next();
System.out.print("Enter last name: ");
String lastName = scan.next();
System.out.print("Enter phone : ");
String phone = scan.next();
// creating Contact object
Contact target = new Contact(firstName, lastName, phone);
Contact index = (Contact)searching.binarySearch(contacts, target);
if(index != null)
System.out.println(target+ " is present");
else
System.out.println(target+" is not available in list");
}
}
/*
Sample run:
Before Sorting:
Kumar, Pravesh 1234567
E, ALex 65345
Xender, Bob 987564
Rahul, Mukesh 3221345
M, Lokesh 098347
After Sorting:
E, ALex 65345
Kumar, Pravesh 1234567
M, Lokesh 098347
Rahul, Mukesh 3221345
Xender, Bob 987564
Enter contact details to be sarch :
Enter first name: Pravesh
Enter last name: Kumar
Enter phone : 1234567
Kumar, Pravesh 1234567 is present
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.