Quicksort algorithm program in Java - 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

Related

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.

Sorting ArrayList of bytes array

I have some code in python which does the following:
for day in server_message.keys():
for epoch in server_message[day].keys():
assert sorted(server_message[day][epoch]) == server_message[day][epoch]
I need to write that code in Java. The problem is that the structure of server_message is as such:
Map<Integer, Map<Integer, ArrayList<byte[]>>>
How can sort ArrayList of bytes? Both Arrays.sort() and Collections.sort() don't return a new sorted array instead the work on the provided array.
Is there anything in Java that I can do to solve this problem, or do I need to write my own sorting algorithm for this kind of sort? How can I compare two bytes array?
Array.sort() uses quick algorithm at back-end and I'm surprised to see why Array.sout() is not working but you can use quick sort for this.
public class QuickSort {
public static void main(String[] args) {
int i;
int[] arr={90,23,101,45,65,23,67,89,34,23};
quickSort(arr, 0, 9);
System.out.println("\n The sorted array is: \n");
for(i=0;i<10;i++)
System.out.println(arr[i]);
}
public static int partition(int a[], int beg, int end)
{
int left, right, temp, loc, flag;
loc = left = beg;
right = end;
flag = 0;
while(flag != 1)
{
while((a[loc] <= a[right]) && (loc!=right))
right--;
if(loc==right)
flag =1;
elseif(a[loc]>a[right])
{
temp = a[loc];
a[loc] = a[right];
a[right] = temp;
loc = right;
}
if(flag!=1)
{
while((a[loc] >= a[left]) && (loc!=left))
left++;
if(loc==left)
flag =1;
elseif(a[loc] <a[left])
{
temp = a[loc];
a[loc] = a[left];
a[left] = temp;
loc = left;
}
}
}
returnloc;
}
static void quickSort(int a[], int beg, int end)
{
int loc;
if(beg<end)
{
loc = partition(a, beg, end);
quickSort(a, beg, loc-1);
quickSort(a, loc+1, end);
}
}
}
I think your problem is to sort an array of bytes (byte[]) and not a list of arrays of bytes (List<byte[]>) which doesn't make any sense.
If you want to get a sorted array of bytes without modifying the existing one you can clone the original array before :
byte[] bytes = {0, 23, 127, -12 };
byte[] clone = bytes.clone();
Arrays.sort(clone);

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);

Binary search does not work with doubles

This program works very well with integers, but not doubles. There are no errors, but the program returns -1. Sorry if this is a stupid question, but I am new to programming.
public class binarySearchProject
{
public static int binarySearch(double[] arr, double x, int high, int low)
{
int mid=(high+low)/2;
if(high==low || low==mid || high==mid)
{
return -1;
}
if(arr[mid]>x)
{
return binarySearch(arr, x, high, mid);
}
else if(arr[mid]<x)
{
return binarySearch(arr, x, mid, low);
}
else if(arr[mid]==x)
{
return mid;
}
return -1;
}
public static void main(String args[])
{
double i = 45.3;
double[] a = {-3, 10, 5, 24, 45.3, 10.5};
int size = a.length;
System.out.println(binarySearch(a, i, size, 0));
}
}
You should change the conditions:
if (arr[mid] > x) should be if (arr[mid] < x)
else if (arr[mid] < x) should be else if (arr[mid] > x)
Also note that in order to make this work, the array must be sorted (That's the whole point of binary search), you can use Arrays#sort:
Arrays.sort(a);
I recommend you rename your class so it begins with an upper case (Following Java Naming Conventions).
As #tobias_k pointed out:
For binary search to work, you need to sort the array first.
See Wikipedia for details.

Categories