SlideShare a Scribd company logo
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 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++)
{
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,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] + ", ");
}
}
}
//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/////////////////////////////////////

More Related Content

DOCX
DS LAB RECORD.docx
PDF
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
PDF
ISCP internal.pdf
PDF
Java Assignment.Implement a binary search algorithm on an array..pdf
PDF
–PLS write program in c++Recursive Linked List OperationsWrite a.pdf
DOCX
Data Structures Using C Practical File
PDF
ArrayTest.java import java.util.Arrays; import java.util.Scann.pdf
PDF
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
DS LAB RECORD.docx
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
ISCP internal.pdf
Java Assignment.Implement a binary search algorithm on an array..pdf
–PLS write program in c++Recursive Linked List OperationsWrite a.pdf
Data Structures Using C Practical File
ArrayTest.java import java.util.Arrays; import java.util.Scann.pdf
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf

Similar to Refer to my progress on this assignment belowIn this problem you w.pdf (20)

PPTX
Bubble sort, Selection sort SORTING .pptx
PDF
Given the following codepackage data1;import java.util.;p.pdf
PDF
Problem 1 Show the comparison of runtime of linear search and binar.pdf
PPTX
sorting and its types
PDF
Sorting_Algoritm-computee-scienceggn.pdf
DOCX
QA Auotmation Java programs,theory
PDF
Using Arrays with Sorting and Searching Algorithms1) This program .pdf
DOCX
cs3381-object oriented programming-ab-manual
PDF
C programs
PDF
ReversePoem.java ---------------------------------- public cl.pdf
DOCX
Data structure and algorithm lab spiral (1) (4) (1).docx
PPTX
array lecture engineeringinformatin_technology.pptx
PDF
Illegal numbers.a. Complete the method find which accepts a collec.pdf
PDF
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
DOCX
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
PDF
Simple 27 Java Program on basic java syntax
PDF
C++ Searching & Sorting5. Sort the following list using the select.pdf
PDF
JAVA PRACTICE QUESTIONS v1.4.pdf
PDF
palindrome number armstrong number fibonaccii
PDF
Bubble sort, Selection sort SORTING .pptx
Given the following codepackage data1;import java.util.;p.pdf
Problem 1 Show the comparison of runtime of linear search and binar.pdf
sorting and its types
Sorting_Algoritm-computee-scienceggn.pdf
QA Auotmation Java programs,theory
Using Arrays with Sorting and Searching Algorithms1) This program .pdf
cs3381-object oriented programming-ab-manual
C programs
ReversePoem.java ---------------------------------- public cl.pdf
Data structure and algorithm lab spiral (1) (4) (1).docx
array lecture engineeringinformatin_technology.pptx
Illegal numbers.a. Complete the method find which accepts a collec.pdf
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Simple 27 Java Program on basic java syntax
C++ Searching & Sorting5. Sort the following list using the select.pdf
JAVA PRACTICE QUESTIONS v1.4.pdf
palindrome number armstrong number fibonaccii
Ad

More from arishmarketing21 (20)

PDF
A series RL circuit includes a 9.05-V battery, a resistance of R = 0.pdf
PDF
What is the dangling pointer Explain with a proper example.Solut.pdf
PDF
Write a function in javascript that calculates the average element i.pdf
PDF
Which a not a likely location of a bacterial to be found Atheroscle.pdf
PDF
What’s Love Got To Do With ItThe Evolution of Human MatingB.pdf
PDF
What is the Surface characterization techniques of Fourier-transform.pdf
PDF
What is the running time complexity and space complexity of the follo.pdf
PDF
A species has a diploid number of chromosomes of 6. If a cell from a.pdf
PDF
What are the security requirements and challenges of Grid and Cloud .pdf
PDF
Using the man command, determine which ls command option (flag) will.pdf
PDF
There a six seats in a bar. Your friend took the second seat from th.pdf
PDF
The basic economic problem is that we only have so many resources, b.pdf
PDF
The organization of interrupted genes is often conserved between spe.pdf
PDF
The daisy has which inflorescence morphology type campanulte tubul.pdf
PDF
Suppose that CaO is present as an impurity to Li2O. The Ca2+ ion sub.pdf
PDF
Resistance A primitive adaptive immune Zone of inhibition The ability.pdf
PDF
Q1) Show what part of SSL that protects against the following attack.pdf
PDF
public class Patient extends Person {=========== Properties ====.pdf
PDF
8. A human T lymphocyte is infected by a HIV. The viral genome prese.pdf
PDF
please help with java questionsJAVA CODEplease check my code and.pdf
A series RL circuit includes a 9.05-V battery, a resistance of R = 0.pdf
What is the dangling pointer Explain with a proper example.Solut.pdf
Write a function in javascript that calculates the average element i.pdf
Which a not a likely location of a bacterial to be found Atheroscle.pdf
What’s Love Got To Do With ItThe Evolution of Human MatingB.pdf
What is the Surface characterization techniques of Fourier-transform.pdf
What is the running time complexity and space complexity of the follo.pdf
A species has a diploid number of chromosomes of 6. If a cell from a.pdf
What are the security requirements and challenges of Grid and Cloud .pdf
Using the man command, determine which ls command option (flag) will.pdf
There a six seats in a bar. Your friend took the second seat from th.pdf
The basic economic problem is that we only have so many resources, b.pdf
The organization of interrupted genes is often conserved between spe.pdf
The daisy has which inflorescence morphology type campanulte tubul.pdf
Suppose that CaO is present as an impurity to Li2O. The Ca2+ ion sub.pdf
Resistance A primitive adaptive immune Zone of inhibition The ability.pdf
Q1) Show what part of SSL that protects against the following attack.pdf
public class Patient extends Person {=========== Properties ====.pdf
8. A human T lymphocyte is infected by a HIV. The viral genome prese.pdf
please help with java questionsJAVA CODEplease check my code and.pdf
Ad

Recently uploaded (20)

PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Institutional Correction lecture only . . .
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
master seminar digital applications in india
PPTX
Lesson notes of climatology university.
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Cell Types and Its function , kingdom of life
PDF
RMMM.pdf make it easy to upload and study
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Classroom Observation Tools for Teachers
Module 4: Burden of Disease Tutorial Slides S2 2025
Final Presentation General Medicine 03-08-2024.pptx
Institutional Correction lecture only . . .
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Microbial diseases, their pathogenesis and prophylaxis
VCE English Exam - Section C Student Revision Booklet
master seminar digital applications in india
Lesson notes of climatology university.
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Complications of Minimal Access Surgery at WLH
Cell Types and Its function , kingdom of life
RMMM.pdf make it easy to upload and study
Computing-Curriculum for Schools in Ghana
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Microbial disease of the cardiovascular and lymphatic systems
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Anesthesia in Laparoscopic Surgery in India
Classroom Observation Tools for Teachers

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