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);
Related
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.
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);
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);
}
}
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 9 years ago.
I'm trying to print a histogram, but am having trouble piecing it all together in main. I'm new to arrays, so if anyone can help with this, it'd be much appreciated. Here are my methods:
public static void main(String[] args) {
randomIntArray(5);
}
public static int randomInt(int low, int high){
int x= (int)(Math.random ()*high)+low;
return x;
}
public static int[] randomIntArray(int n){
int[] a = new int [n];
for (int i = 0;i<a.length;i++){
a[i]=randomInt (0,100);
}
System.out.println(printHist(a));
return a;
}
public static int[] printHist(int[]a){
int[] k = new int[11];
int i=0;
while (i<=10) {
int counter = 0;
int h=0;
while(h<a.length) {
if (a[h] == i) {
counter++;
h++;
}
h++;
}
k[i] = counter;
i++;
}
return k;
}
And here's what I get as output.
[I#fb53f6
Do I need to rethink the way I'm doing this, or is there a simple fix?
System.out.println(arrayObject) does not do what you think it does.
Try one of the solutions at this related question: What's the simplest way to print a Java array? - such as Arrays.toString(arrayObject)
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