import java.util.Arrays;
public class lab {
public static void main(String args[])
{
int arr[] = {10, 7, 8, 9, 1, 5,6,7};
int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};
int arr3[] = {1, 3, 5, 3, 2, 6, 20};
quicksort(arr,0,arr.length-1);
quicksort(arr2,0,arr2.length-1);
quicksort(arr3,0,arr3.length-1);
System.out.println(Arrays.toString(arr));
System.out.println(Arrays.toString(arr2));
System.out.println(Arrays.toString(arr3));
}
private static int partition(int[] items,int low, int
high)
{
int i=0;
int j=0;
int k=high+1;
int pivot=0;
while(j < k)
{
if (items[j] {
swap(j,i,items);
j++;
i++;
}
else if(items[j]==pivot)
{
j++;
}
else
{
swap(j,k-1,items);
k--;
}
}
return i;
}
private static void swap(int i,int j,int[]
items)
{
int temp=items[i];
items[i]=items[j];
items[j]=temp;
}
public static void quicksort(int[] arr)
{
quicksort(arr,0,arr.length-1);
}
private static void quicksort(int[] arr, int low, int
high)
{
if (low < high)
{
/* pi is partitioning index, arr[pi] is
now at right place */
int pi = partition(arr,low,high);
// Recursively sort elements before
// partition and after partition
quicksort(arr, low, pi-1);
quicksort(arr, pi+1, high);
}
}
}
This code has a stackoverflow error and I don't know what to do to fix it. If you could point out what's wrong and how to fix it, I would appreciate it,
Working fine now, Check and Let me know...PLEASE COMMENT
-------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Arrays;
public class lab {
public static void main(String args[]) {
int arr[] = { 10, 7, 8, 9, 1, 5, 6, 7 };
int arr2[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
int arr3[] = { 1, 3, 5, 3, 2, 6, 20 };
quicksort(arr, 0, arr.length - 1);
quicksort(arr2, 0, arr2.length - 1);
quicksort(arr3, 0, arr3.length - 1);
System.out.println(Arrays.toString(arr));
System.out.println(Arrays.toString(arr2));
System.out.println(Arrays.toString(arr3));
}
private static int partition(int[] items, int low, int high) {
int pivot = items[high];
int i = (low - 1);
int j = low;
while( j < high) {
if (items[j] <= pivot) {
i++;
int temp = items[i];
items[i] = items[j];
items[j] = temp;
}
j++;
}
swap(i+1, high, items);
return i + 1;
}
private static void swap(int i, int j, int[] items) {
int temp = items[i];
items[i] = items[j];
items[j] = temp;
}
public static void quicksort(int[] arr) {
quicksort(arr, 0, arr.length - 1);
}
private static void quicksort(int[] arr, int low, int high) {
if (low < high) {
/*
* pi is partitioning index, arr[pi] is now at right place
*/
int pi = partition(arr, low, high);
// Recursively sort elements before
// partition and after partition
quicksort(arr, low, pi - 1);
quicksort(arr, pi + 1, high);
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
See Output
Thanks, PLEASE COMMENT if there is any concern.
import java.util.Arrays; public class lab { public static void main(String args[]) { int...
must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...
import java.util.Scanner; public class TriangleMaker { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle."); Scanner keyboard = new Scanner(System.in); int size = keyboard.nextInt(); for (int i = 1; i <= size; i++) { for (int j = 0; j < i; j++) { System.out.print("*"); } System.out.println(); } for (int...
The goal is to generate some random number and output a sorted list by quicksort. what do I need to add in my main to accomplish that? i also want it to output the run time. thank you #include "pch.h" #include <iostream> using namespace std; /* C implementation QuickSort */ #include<stdio.h> void swap(int* a, int* b) { int t = *a; *a = *b; *b = t; } int partition(int arr[], int low, int high) { ...
Draw a flowchart for this program public class InsertionSort { public static void main(String a[]) { int[] array = {7, 1, 3, 2, 42, 76, 9}; insertionSort(array); } public static int[] insertionSort(int[] input) { int temp; for (int i = 1; i < input.length; i++) { for (int j = i; j > 0; j--) { if (input[j] < input[j - 1]) { temp = input[j]; input[j] = input[j - 1]; input[j - 1] = temp; } } display(input, i);...
import java.util.Scanner; public class StudentClient { public static void main(String[] args) { Student s1 = new Student(); Student s2 = new Student("Smith", "123-45-6789", 3.2); Student s3 = new Student("Jones", "987-65-4321", 3.7); System.out.println("The name of student #1 is "); System.out.println("The social security number of student #1 is " + s1.toString()); System.out.println("Student #2 is " + s2); System.out.println("the name of student #3 is " + s3.getName()); System.out.println("The social security number...
JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers: 47 71 15 35 66 61 44 26 68 56 18 19 36 84 69 55 1. Find the value of pivot 2. Show the result after partitionIt() is called first time 3. Show the value of partition 4. Show the content of the array ///////////////////////////// Lab6.java class ArrayIns { private long[] theArray; // ref to array theArray private int nElems; // number of...
Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp { public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...
Cant figure out how to fix error Code- import java.io.File; import java.io.IOException; import java.util.*; public class Program8 { public static void main(String[] args)throws IOException{ File prg8 = new File("program8.txt"); Scanner reader = new Scanner(prg8); String cName = ""; int cID = 0; double bill = 0.0; String email = ""; double nExempt = 0.0; String tExempt = ""; int x = 0; int j = 1; while(reader.hasNextInt()) { x = reader.nextInt();} Customers c1 [] = new Customers [x]; for (int...
c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...
public class Test { private static int i =0; private static int j =0; public static void main(String[] args) { int i = 2; int k = 3; { int j =3; System.out.println("i + j is : " + i + j); } k = i + j; System.out.println("K is " + k ); System.out.println("K is " + j); } } why is the output i + j = 23 K =2 K =0 Please explain a step by step...