Having some problems implementing quicksort in java. I get a stackoverflow error when I run this program and I’m not exactly sure why. If anyone can point out the error, it would be great. si is the starting index. ei is the ending index. Answer First you should fix the bounds of the qsort recursive call as suggested by Keith,
Tag: sorting
Sorting in java for array only containing 0 and 1
How to sort array Answer use any sorting algorithm to do it. For beginner use bubble sort (easy to understand) Refer Wiki EDITED As @Pradeep Said: You may definitely use Array.sort()
Get Minimum and maximum numbers from a list
Assuming we have a list containing integers and we need to find min & max. What is the best way to do it ? I can think of following : If number of reads are much higher than writes; keep the list in sorted manner. Whenever a number is added; add it in sorted manner. Now to get min and
Mergesort in java
I am new to Java and have tried to implement mergesort in Java. However, even after running the program several times, instead of the desired sorted output, I am getting the same user given input as …
Why does QuickSort use O(log(n)) extra space?
I have implemented the below quicksort algorithm. Online I’ve read that it has a space requirement of O(log(n)). Why is this the case? I’m not creating any extra data structures. Is it because my recursion will use some extra space on the stack? If this is the case, is it possible to do it with less memory by not having
difference between natural ordering and total ordering
I happen to come across many statements like comparable is used when natural ordering is required while sorting an array or collection and comparator for total ordering. The version you may have heard could be same or different with the same meaning but ultimately its one of the distinguishing factors between the two(comparator and comparable interfaces). But, I couldn’t find
Why is there no SortedList in Java?
In Java there are the SortedSet and SortedMap interfaces. Both belong to the Java Collections framework and provide a sorted way to access the elements. However, in my understanding there is no SortedList in Java. You can use java.util.Collections.sort() to sort a list. Any idea why it is designed like that? Answer List iterators guarantee first and foremost that you
Sort ArrayList of strings by length
I want to order an ArrayList of strings by length, but not just in numeric order. Say for example, the list contains these words: They need to be ordered by their difference in length to a special string, for example: So the final list would look like this (difference in brackets): Answer Use a custom comparator: Then sort the list
How can i sort a string without using sort() method in java?
I want to sort a string “computer” -> “cemoprtu”, but without using Arrays.sort(string). Answer Looks like you need to sort the characters, so I’d start with
Java – Collections.sort() performance
I’m using Collections.sort() to sort a LinkedList whose elements implements Comparable interface, so they are sorted in a natural order. In the javadoc documentation its said this method uses mergesort algorithm which has n*log(n) performance. My question is if there is a more efficient algorithm to sort my LinkedList? The size of that list could be very high and sort