Quick sort not working - java

while running above code the first pivot is returned as 3.This pivot is transmitted as 2 in first recursion method but in second recursion it does not take value 4. can some one identify what is problem.
class QuickSortRevision{
int pivot;
void QuickSort(int[] arr,int low,int high){
if(low>=high)
return;
pivot = quickSortPivot(arr,low,high);//first execution pivot =3
QuickSort(arr,low,pivot-1);//this is taking 0,2 as parameter;
QuickSort(arr,pivot+1,high);//but this is not taking 4,8 as parameter;
}
int quickSortPivot(int[] arr,int low,int high){
int temp,index,partition,lindex,hindex;
lindex=low;
hindex=high-1;
partition = arr[high];
index=high;
System.out.println("low : "+low+" high "+high);
while(lindex!=hindex){
while(arr[lindex]<partition && (lindex!=hindex) ){
lindex++;
}
while(arr[hindex]>partition && (lindex!=hindex) ){
hindex--;
}
if( lindex!=hindex)
{
temp=arr[lindex];
arr[lindex]=arr[hindex];
arr[hindex]=temp;
lindex++;hindex--;
}
}
temp=arr[lindex];
arr[lindex]=partition;
arr[index]=temp;
System.out.println("lindex: "+lindex);
return lindex;
}
void printArray(int[] arr)
{
for(int element : arr)
System.out.print(" "+element);
}
public static void main(String[] args){
QuickSortRevision qs = new QuickSortRevision();
int arr[]={17,41,5,22,54,6,29,3,13};
qs.QuickSort(arr,0,arr.length-1);
qs.printArray(arr);
}}

The first time you call QuickSort the class member pivotis assigned the value 3. Then a recursive call to QuickSort invokes quickSortPivot whose result is assigned to pivot (and further recursive calls also modify this value). When this call to Quicksort returns, the value of pivothas been modified!
You shoud declre pivot as a variable of the method QuickSort, not an instance variable of the class QuickSortRevision
PS: the function QuickSort should be called quickSort

replace you code lindex!=hindex to lindex<=hindex.
because there a sence like lindex > hindex.
The code like this:
public class QuickSortRevision{
int pivot;
static int id = 1;
void QuickSort(int[] arr,int low,int high){
if(low>=high)
return;
pivot = quickSortPivot(arr,low,high);//first execution pivot =3
QuickSort(arr,low,pivot-1);//this is taking 0,2 as parameter;
QuickSort(arr,pivot+1,high);//but this is not taking 4,8 as parameter;
}
int quickSortPivot(int[] arr,int low,int high){
int temp,index,partition,lindex,hindex;
lindex=low;
hindex=high - 1;
partition = arr[high];
index=high;
while(lindex <= hindex){
while(arr[lindex]<partition && (lindex<=hindex) ){
lindex++;
}
while(arr[hindex]>partition && (lindex<=hindex) ){
hindex--;
}
System.out.println("low : "+low+" high "+high);
if( lindex<=hindex)
{
temp=arr[lindex];
arr[lindex]=arr[hindex];
arr[hindex]=temp;
lindex++;hindex--;
}
}
temp=arr[lindex];
arr[lindex]=partition;
arr[index]=temp;
// System.out.println(lindex+" "+arr[lindex]);
System.out.println("lindex: "+lindex);
return lindex;
}
void printArray(int[] arr)
{
for(int element : arr)
System.out.print(" "+element);
}
public static void main(String[] args){
QuickSortRevision qs = new QuickSortRevision();
int arr[]={17,41,5,22,54,6,29,3,13};
qs.QuickSort(arr,0,arr.length-1);
qs.printArray(arr);
}
}

Related

Clarification on tail recursive methods

Is the following method tail-recursive?
I believe that it is not tail recursive because it relies on the previous results and so needs a stack frame, am I correct to state this?
public int[] fib(int n)
{
if(n <= 1){
return (new int[]{n,0});
}
else{
int[] F = fib(n-1);
return (new int[]{F[0]+ F[1], F[0]});
}
}
You are correct: It is not tail recursive because the last line is not of the form
return funcName(args);
Yes, you are correct, since it does not end with a call to itself of the form of
return fib(somevalue);
To convert it into a tail-recursive version you could do something like
// Tail Recursive
// Fibonacci implementation
class GFG
{
// A tail recursive function to
// calculate n th fibonacci number
static int fib(int n, int a, int b )
{
if (n == 0)
return a;
if (n == 1)
return b;
return fib(n - 1, b, a + b);
}
public static void main (String[] args)
{
int n = 9;
System.out.println("fib(" + n +") = "+
fib(n,0,1) );
}
}
Code taken from https://www.geeksforgeeks.org/tail-recursion-fibonacci/

why is this not executing in vs code editor?

public class binsearch {
public static void main(String args[])
{
int arr[]={2,45,-21,56,23};
int target=45;
int answer=binarysearch(arr, target);
System.out.println(answer);
}
static int binarysearch(int arr[],int target)
{
int start=0;
int end=arr.length-1;
int mid=start+end/2;
while(start<=end)
{
if(target<arr[mid])
{
mid=end-1;
}
else if(target>arr[mid])
{
mid=mid+1;
}
else
{
return mid;
}
}
return -1;
}
}
I have tried running this code multiple times but it just doesnt run. I dont think there is any problem with the logic for binary search in this code. Please do help.Thank you.
Your code runs. It has an infinite loop so it never terminates.
In order for a binary search to work. The array must be sorted in ascending order. So just sort the array before you do the binary search. Below code uses class java.util.Arrays to sort the array but you can sort it anyway you like. Just make sure that the array is sorted before you do the binary search.
Also, the calculation of mid needs to be inside the while loop because it always changes since its value is determined by the values of both start and end and those values are changed inside the while loop.
Note that I changed the name of the class so as to adhere to Java naming conventions. The conventions make it easier for other people to read and understand your code.
import java.util.Arrays;
public class BinSearch {
static int binarysearch(int arr[], int target) {
int start = 0;
int end = arr.length - 1;
while (start <= end) {
int mid = start + ((end - start) / 2);
if (target < arr[mid]) {
end = mid - 1;
}
else if (target > arr[mid]) {
start = mid + 1;
}
else {
return mid;
}
}
return -1;
}
public static void main(String[] args) {
int arr[] = {2, 45, -21, 56, 23};
Arrays.sort(arr);
int target = 45;
int answer = binarysearch(arr, target);
System.out.println(answer);
}
}
The answer is 3 because, after the sort, 45 is the second last element in the [sorted] array because it is the second largest number in the array.
If you want to search without sorting the array then a binary search is not appropriate.

Max Heap Implementation

What corrections should I make to the following MaxHeap Implementation in Java?The Insert function keeps running when called. What is the error in the Insert and the BuildHeap functions?I have edited the PercolateDown function. I think it should be correct now..?
public class MaxHeap {
public int[] array;
public int count;
public int capacity;
public MaxHeap(int capacity){
this.capacity=capacity;
this.count=0;
this.array=new int[capacity];
}
public int Parent(int i){
if(i<=0 || i>=this.count)
return -1;
return
(i-1)/2;
}
public int LeftChild(int i){
int left=2*i+1;
if(left>=this.count)
return -1;
return left;
}
public int RightChild(int i){
int right=2*i+2;
if(right>=this.count)
return -1;
return right;
}
public int GetMaximum(){
if(this.count==0)
return -1;
return this.array[0];
}
public void PercolateDown(int i){
int l,r,max,temp;
l=LeftChild(i);
r=RightChild(i);
if(l!=-1 && this.array[l]>this.array[i])
max=l;
else
max=i;
if(r!=-1 && this.array[r]>this.array[max])
max=r;
if(max!=i){
temp=this.array[i];
this.array[i]=this.array[max];
this.array[max]=temp;
}
if(max==i){return;}
PercolateDown(max);
}
public int DeleteMax(){
if(this.count==0)
return -1;
int data=this.array[0];
this.array[0]=this.array[this.count-1];
this.count--;
PercolateDown(0);
return data;
}
public void Insert(int data){
int i;
if(this.count==this.capacity){
ResizeHeap();
}
this.count++;
i=this.count-1;
while(i>=0 && data>this.array[(i-1)/2]){
this.array[i]=this.array[(i-1)/2];
i=(i-1)/2;
}
this.array[i]=data;
}
public void ResizeHeap(){
int[] array_old=new int[this.capacity];
for(int i=0;i<this.capacity;i++){
array_old[i]=this.array[i];
}
this.array=new int[this.capacity*2];
for(int i=0;i<this.capacity;i++){
this.array[i]=array_old[i];
}
this.capacity*=2;
array_old=null;
}
public static MaxHeap BuildHeap(int[]A,int n){
MaxHeap h=new MaxHeap(n*2);
if(A==null)return h;
//while(n>h.capacity)
//h.ResizeHeap();
h.capacity=n*2;
for(int i=0;i<n;i++){
h.array[i]=A[i];
}
h.count=n;
for(int i=(n/2)-1;i>=0;i++){
h.PercolateDown(i);
}
return h;
}
public void Delete(int i){
if(this.count<i){
System.out.println("Wrong Position");
return;
}
this.array[i]=this.array[this.count-1];
this.count--;
PercolateDown(i);
}
public static void main(String[] args){
//int[] A={7,6,5,4,3,2,1};
//int len=A.length;
//MaxHeap h=BuildHeap(A,len);
//for(int i=0;i<len;i++){
//System.out.print(h.array[i]+" ");
//}
MaxHeap h=new MaxHeap(10);
h.Insert(7);
System.out.print(h.array[0]);
}
}
I inserted this statement inside the while loop in Insert():
System.out.println(i);
It prints 0 every time. Since 0 >= 0 and also your array comes with 0s in it, so when data is 7 and 7 > 0, the while condition is true. Inside your loop you are setting i to (i - 1) / 2, this is -1 / 2, which is rounded towards zero, yielding 0 again. So i is not changed, and your while condition continues to be true. This is why your loop never stops. I have not understood how you had intended the method to work, so I dare not give suggestions.
Trying the same print statement inside the for loop of BuildHeap() reveals that i is ever increasing until it overflows and suddenly is negative (if you add one to the highest int you can have, you get the lowest possible negative value, -2147483648). I think you had intended i-- rather then i++ in for(int i=(n/2)-1;i>=0;i++){.

Displaying values of array [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 8 years ago.
[after edit 1] I have seen the other answers here in SO, and from looking at my code, I find that I am adhering to the principle of Java's pass-by-value-of-reference. But still my array is not getting sorted. Please can someone point out any errors I am making in my code?
[after edit 2] Found the problem. It was nothing to do with array passing. In my merge method, it should be if (end-start<=0) not the other way round!
I am trying to implement mergesort. However, I am unable to display values of my array and am unsure of how to pass my arrays in Java such that the original array can be modified.
How can I modify my current code to display the values of the sorted array?
I understand java passes the copy of the array's reference around, but doesnt this mean that the original array gets modified?
CODE:
I am calling mergesort method from my main method.
public static void main(String[] args) {
int[] qn = {10,22,33,4,5,6,1};
qn= mergesort(qn,0,qn.length-1);
for (int i=0;i<qn.length;i++){ //print to see values if sorted/not
System.out.print(qn[i]+ " ");
}
}
public static int[] mergesort(int[] arr,int start,int end){
int mid = (end+start)/2;
if (end - start<=0){
return arr;
}
else if (end-start>=1){
arr=mergesort(arr,start,mid);
arr=mergesort(arr,mid+1,end);
}
arr=merge(arr,start,end);
for (int i=0;i<arr.length;i++){
System.out.print(arr[i]+ " ");
}
System.out.println();
return arr;
}
public static int[] merge(int[] arr,int start,int end){
int mid= (start+end)/2;
if(start-end<=0){
return arr;
}
int a= start; int b = mid+1;
while (a<=mid && b <=end){
if (arr[b]<arr[a]){
int tmp = arr[b++];
for(int i=++mid;i>a;i--){
arr[i]=arr[i-1];
}
arr[a++]=tmp;
}
else if (arr[b]>arr[a]){
a++;
}
else{ //arr[b]=arr[a]
if(a==mid && b == end){
break; //all between mid and end will be equal too
}
int tmp= arr[b++];
a++;
for (int i=++mid;i>a;i--){
arr[i]=arr[i-1];
}
arr[a++]=tmp;
//a++;
//b++;
}
}
return arr;
}
}
modified my answer based on Manu's below. but it's still not working. Attaching code below:
public static void mergesort(int[] arr,int start,int end){
int mid = (end+start)/2;
if (end - start<=0){
return;
}
else if (end-start>=1){
mergesort(arr,start,mid);
mergesort(arr,mid+1,end);
}
merge(arr,start,end);
for (int i=0;i<arr.length;i++){
System.out.print(arr[i]+ " ");
}
System.out.println();
return;
}
public static void merge(int[] arr,int start,int end){
int mid= (start+end)/2;
if(start-end<=0){
return;
}
int a= start; int b = mid+1;
while (a<=mid && b <=end){
if (arr[b]<arr[a]){
int tmp = arr[b++];
for(int i=++mid;i>a;i--){
arr[i]=arr[i-1];
}
arr[a++]=tmp;
}
else if (arr[b]>arr[a]){
a++;
}
else{ //arr[b]=arr[a]
if(a==mid && b == end){
break; //all between mid and end will be equal too (pearl)
}
int tmp= arr[b++];
a++;
for (int i=++mid;i>a;i--){
arr[i]=arr[i-1];
}
arr[a++]=tmp;
//a++;
//b++;
}
}
return;
}
}
You are right, in Java, the copy of the reference of the array gets passed to the function. However, both references point to the same object (the array in your case). This means if you change the values of the array in your merge-method, the values of the array in your mergesort-method changes too. Reqrite your code to this:
public static void mergesort(int[] arr, ...
public static void merge(int[] arr, ...
and simply call
merge(arr, start, mid);

Quicksort algorithm program in Java

I'm trying to implement QuickSort algorithm program in Java, but I'm getting incorrect answer.
public class QuickSort {
public static void main(String[] args){
int arr[]={12,34,22,64,34,33,23,64,33};
int i=0;
int j=arr.length;
while(i<j){
i=quickSort(arr,i,i+1,j-1);
}
for(i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
}
public static int quickSort(int arr[],int pivot,int i,int j){
if(i>j) {
swap(arr,pivot,j);
return i;
}
while(i<arr.length&&arr[i]<=arr[pivot]) {
i++;
}
while(j>=1&&arr[j]>=arr[pivot]) {
j--;
}
if(i<j)
swap(arr,i,j);
return quickSort(arr,pivot,i,j);
}
public static void swap(int[] arr,int i,int j) {
int temp;
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
The above program giving me the output as: 12 23 22 33 34 33 64 34 64
Could anyone please tell me how can I get my desire result?
The problem is that this is not really how quicksort works. Quicksort is a recursive algorithm that should only be called once from outside of itself. The idea is that at each iteration, you partition the array into two halves - the left half contains all elements less than the pivot, and the right half contains all elements greater than / equal to the pivot. Then you quicksort the two halves, and finally put the pivot in the middle.
If the side that you are quicksorting is less than 3 elements long, you can just swap the two elements or leave them, and that part of the array is done.
But it doesn't look like your code is doing that at all - you are calling Quicksort 6 times from your client, and within the quicksort function you are making at most one swap. So this is not a case where someone is going to be able to look at your code and debug it by telling you to move a swap or something. You need to revisit your logic.
Check out the Wikipedia diagram for a visual example of what is supposed to happen in a single iteration:
http://en.wikipedia.org/wiki/File:Partition_example.svg
There are open source implementations of quicksort in Apache Harmony and Apache Mahout, probably amongst many others. You can read them.
public static int partition(int[] a, int p, int r){
int i=p,j=r,pivot=a[r];
while(i<j){
while(i<r && a[i] <= pivot){
i++;
}
while(j>p && a[j]>pivot){
j--;
}
if(i<j){
swap(a, i, j);
}
}
return j;
}
public static void quickSort(int[] a, int p, int r){
if(p<r){
int q=partition(a, p, r);
if(p==q){
quickSort(a, p+1, r);
}else if(q==r){
quickSort(a, p, r-1);
}else {
quickSort(a, p, q);
quickSort(a, q+1, r);
}
}
}
public static void swap(int[] a, int p1, int p2){
int temp=a[p1];
a[p1]=a[p2];
a[p2]=temp;
}
here is a quicksort algorithm
package drawFramePackage;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Random;
public class QuicksortAlgorithm {
ArrayList<AffineTransform> affs;
ListIterator<AffineTransform> li;
Integer count, count2;
/**
* #param args
*/
public static void main(String[] args) {
new QuicksortAlgorithm();
}
public QuicksortAlgorithm(){
count = new Integer(0);
count2 = new Integer(1);
affs = new ArrayList<AffineTransform>();
for (int i = 0; i <= 128; i++){
affs.add(new AffineTransform(1, 0, 0, 1, new Random().nextInt(1024), 0));
}
affs = arrangeNumbers(affs);
printNumbers();
}
public ArrayList<AffineTransform> arrangeNumbers(ArrayList<AffineTransform> list){
while (list.size() > 1 && count != list.size() - 1){
if (list.get(count2).getTranslateX() > list.get(count).getTranslateX()){
list.add(count, list.get(count2));
list.remove(count2 + 1);
}
if (count2 == list.size() - 1){
count++;
count2 = count + 1;
}
else{
count2++;
}
}
return list;
}
public void printNumbers(){
li = affs.listIterator();
while (li.hasNext()){
System.out.println(li.next());
}
}
}
also available with description at nathan's computer knowledge with a description
[code]
[/code]
``
Your loop is not working properly. Refer the code which is solve your problem about Quick Sort
static void quickSort (int[] numbers, int low, int high)
{
int i=low;
int j=high;
int temp;
int middle=numbers[(low+high)/2];
while (i<j) {
while (numbers[i]<middle) {
i++;
}
while (numbers[j]>middle) {
j--;
}
if (i<=j) {
temp=numbers[i];
numbers[i]=numbers[j];
numbers[j]=temp;
i++;
j--;
}
}
if (low<j) {
quickSort(numbers, low, j);
}
if (i<high) {
quickSort(numbers, i, high);
}
}
Refer Quick sort.
Please find comprehensive working code for quick sort algorithm implemented in Java here,
http://tech.bragboy.com/2010/01/quick-sort-in-java.html

Categories