Refer to my progress on this assignment below
In this problem you will make it “more” object-oriented in the following ways:
-You will change its name to SortedList
-You will change the constructor that takes no arguments to be more traditional and initialize the
member fields to dummy values.
-You will add a constructor that takes in an initialized array and a size
-You will add an insert function that adds a value to the list and maintains its sorted-ness
-You will add a quicksort function check the below code.
-You will make updates as necessary to the main function so that it still runs and tests your code.
The code below seems to sort correctly but it skips over some items in the array and I am not
sure what is happening. Please help! Thanks!
import java.util.Scanner;
class SortedList
{
private static int array[];
private static int n;
public SortedList()
{
Scanner in = new Scanner(System.in);
System.out.println(\"Enter number of elements\");
n = in.nextInt();
array = new int[n];
if(n == 0) {
System.out.print(\"Since no arguments array set to: \");
} else {
System.out.print(\"Creating array size \" + n + \": \");
}
for(int i = 0; i < n; i++)
{
array[i] = 0;
}
//System.out.println(\"Enter \" + n + \" integers in ascending order\");
/*for (c = 0; c < n; c++)
array[c] = in.nextInt();*/
for(int i = 0; i < n; i++)
{
System.out.print(array[i] + \" \");
}
System.out.print(\"\ \");
}
public SortedList(int a[], int size)
{
array = a;
n = size;
}
public int binsearch(int search)
{
int first, last, middle;
first = 0;
last = n - 1;
middle = (first + last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
//System.out.println(search + \" found at location \" + (middle + 1) + \".\");
return middle+1;//+1 for the non-CS people who don\'t start counting at zero.
}
else
last = middle - 1;
middle = (first + last)/2;
}
return -1;
//System.out.println(search + \" is not present in the list.\ \");
}
public static int partition(int input[], int p, int r)
{
int pivot = input[r];
while(p < r)
{
while(input[p] < pivot)
{
p++;
}
while(input[r] > pivot)
{
r--;
}
if(input[p] == input[r])
{
p++;
}
else if(p < r)
{
int tmp = input[p];
input[p] = input[r];
input[r] = tmp;
}
}
return r;
}
public static void quicksort(int input[], int p, int r)
{
if(p < r)
{
int j = partition(input, p, r);
quicksort(input, p, j-1);
quicksort(input, j+1, r);
}
}
public static void insert(int value, int cell)
{
array[cell] = value;
quicksort(array, 0, n-1);
}
public static void main(String args[])
{
int c;
Scanner in = new Scanner(System.in);
SortedList b = new SortedList();
System.out.println(\"Input numbers\");
for(int i = 0; i < n; i++)
{
c = in.nextInt();
in.nextLine();
insert(c, i);
}
in.close();
for(int j = 0; j < n; j++)
{
System.out.print(array[j] + \", \");
}
}
}
Solution
//the whole program that you wrote is correct except one line , when you call the quicksort
everytime you insert a //value dont pass n pass the i.
Refer to my progress on this assignment belowIn this problem you w.pdf
1. Refer to my progress on this assignment below
In this problem you will make it “more” object-oriented in the following ways:
-You will change its name to SortedList
-You will change the constructor that takes no arguments to be more traditional and initialize the
member fields to dummy values.
-You will add a constructor that takes in an initialized array and a size
-You will add an insert function that adds a value to the list and maintains its sorted-ness
-You will add a quicksort function check the below code.
-You will make updates as necessary to the main function so that it still runs and tests your code.
The code below seems to sort correctly but it skips over some items in the array and I am not
sure what is happening. Please help! Thanks!
import java.util.Scanner;
class SortedList
{
private static int array[];
private static int n;
public SortedList()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];
if(n == 0) {
System.out.print("Since no arguments array set to: ");
} else {
System.out.print("Creating array size " + n + ": ");
}
for(int i = 0; i < n; i++)
{
array[i] = 0;
}
//System.out.println("Enter " + n + " integers in ascending order");
/*for (c = 0; c < n; c++)
2. array[c] = in.nextInt();*/
for(int i = 0; i < n; i++)
{
System.out.print(array[i] + " ");
}
System.out.print(" ");
}
public SortedList(int a[], int size)
{
array = a;
n = size;
}
public int binsearch(int search)
{
int first, last, middle;
first = 0;
last = n - 1;
middle = (first + last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
//System.out.println(search + " found at location " + (middle + 1) + ".");
return middle+1;//+1 for the non-CS people who don't start counting at zero.
}
else
last = middle - 1;
middle = (first + last)/2;
}
return -1;
3. //System.out.println(search + " is not present in the list. ");
}
public static int partition(int input[], int p, int r)
{
int pivot = input[r];
while(p < r)
{
while(input[p] < pivot)
{
p++;
}
while(input[r] > pivot)
{
r--;
}
if(input[p] == input[r])
{
p++;
}
else if(p < r)
{
int tmp = input[p];
input[p] = input[r];
input[r] = tmp;
}
}
return r;
}
public static void quicksort(int input[], int p, int r)
{
4. if(p < r)
{
int j = partition(input, p, r);
quicksort(input, p, j-1);
quicksort(input, j+1, r);
}
}
public static void insert(int value, int cell)
{
array[cell] = value;
quicksort(array, 0, n-1);
}
public static void main(String args[])
{
int c;
Scanner in = new Scanner(System.in);
SortedList b = new SortedList();
System.out.println("Input numbers");
for(int i = 0; i < n; i++)
{
c = in.nextInt();
in.nextLine();
insert(c, i);
}
in.close();
for(int j = 0; j < n; j++)
{
System.out.print(array[j] + ", ");
}
}
}
5. Solution
//the whole program that you wrote is correct except one line , when you call the quicksort
everytime you insert a //value dont pass n pass the index (cell) look at the highlighted code
below in your program.
//as you are sorting the entire array every tie 0's are being sorted everytime which are already in
array.
import java.util.Scanner;
class SortedList
{
private static int array[];
private static int n;
public SortedList()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];
if(n == 0) {
System.out.print("Since no arguments array set to: ");
} else {
System.out.print("Creating array size " + n + ": ");
}
for(int i = 0; i < n; i++)
{
array[i] = 0;
}
//System.out.println("Enter " + n + " integers in ascending order");
/*for (c = 0; c < n; c++)
array[c] = in.nextInt();*/
for(int i = 0; i < n; i++)
6. {
System.out.print(array[i] + " ");
}
System.out.print(" ");
}
public SortedList(int a[], int size)
{
array = a;
n = size;
}
public int binsearch(int search)
{
int first, last, middle;
first = 0;
last = n - 1;
middle = (first + last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
//System.out.println(search + " found at location " + (middle + 1) + ".");
return middle+1;//+1 for the non-CS people who don't start counting at zero.
}
else
last = middle - 1;
middle = (first + last)/2;
}
return -1;
//System.out.println(search + " is not present in the list. ");
}
7. public static int partition(int input[], int p, int r)
{
int pivot = input[r];
while(p < r)
{
while(input[p] < pivot)
{
p++;
}
while(input[r] > pivot)
{
r--;
}
if(input[p] == input[r])
{
p++;
}
else if(p < r)
{
int tmp = input[p];
input[p] = input[r];
input[r] = tmp;
}
}
return r;
}
public static void quicksort(int input[], int p, int r)
{
if(p < r)
{
8. int j = partition(input, p, r);
quicksort(input, p, j-1);
quicksort(input, j+1, r);
}
}
public static void insert(int value, int cell)
{
array[cell] = value;
quicksort(array, 0,cell);
}
public static void main(String args[])
{
int c;
Scanner in = new Scanner(System.in);
SortedList b = new SortedList();
System.out.println("Input numbers");
for(int i = 0; i < n; i++)
{
c = in.nextInt();
in.nextLine();
insert(c, i);
}
in.close();
for(int j = 0; j < n; j++)
{
System.out.print(array[j] + ", ");
}
}
}
9. //example output:-
Enter number of elements
5
Creating array size 5: 0 0 0 0 0
Input numbers
5
3
8
1
6
1, 3, 5, 6, 8
///////////////////////////////////thanks/////////////////////////////////////