sorting-algorithms | Sorting algorithms in Java | Learning library
kandi X-RAY | sorting-algorithms Summary
kandi X-RAY | sorting-algorithms Summary
Sorting algorithms in Java
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Heapify the given array
- Removes the element at position i
- Build max heap
- Sort the array
- Swap two elements of the specified array
- B Bubble Sort function
- Iterate sort method
sorting-algorithms Key Features
sorting-algorithms Examples and Code Snippets
Community Discussions
Trending Discussions on sorting-algorithms
QUESTION
been trying to program the following Quicksort Algorithm in Swift for a while now and cannot work out the issue. [Quicksort as there are around 15,000 actual values in array]. The problem is only the left half of the array is ordered (see pic), and the method is never exited (infinite loop). Following a Java conversion from http://www.java2novice.com/java-sorting-algorithms/quick-sort/ (tested in Java and does work). Cannot work out the error.
...ANSWER
Answered 2017-Jan-28 at 12:38Your computation of piv
and pivot_location
is wrong. It should be:
QUESTION
I'm having a problem with a asynchronous function in javascript
My function looks like this:
...ANSWER
Answered 2020-Jan-01 at 21:58I see your .map() function doesn't return any promises. You can fix this with
QUESTION
I'm reading through the khan academy course on algorithms. I'm at https://www.khanacademy.org/computing/computer-science/algorithms/sorting-algorithms/p/challenge-find-minimum-in-subarray
The code challenge is supposed to loop through the array find and return the lowest number in the subarray. I've written code which I think should work, but I'm getting the error in the title. Why?
...ANSWER
Answered 2017-Jun-05 at 19:55The second part of your for loop needs to be something that actually tests for something. right now it's an infinite loop because its something that's always true
This should fix it:
QUESTION
There are many places I have seen where it talks about how Insertion Sort is good for small data sets. I can't find a number for what "small" is though. My guess is that there is no absolute answer and that it depends on the type of machine the code is being run on.
However, what factors go into deciding what is the threshold for when Insertion Sort is a good idea? And what are some ballpark figures for "small"? 5? 10? 50? 100?
Thanks!
Site saying Insertion Sort is good for small data sets: https://www.toptal.com/developers/sorting-algorithms/insertion-sort
...ANSWER
Answered 2018-Dec-16 at 17:28An attempt at an answer, providing we're talking about the general sorting problem. Insertion sort is on average O(n^2), efficient sorting algorithms are on average O(nlogn). So vaguely speaking if something takes K steps to sort efficiently it will take around (kind of) K^2 steps with insertion sort.
So if n > K is too slow for your liking with an efficient sort, n > K^0.5 will be too slow for you (roughly) with insertion sort.
Practically speaking let's say you're happy to sort arrays of size 10^8 with something efficient then you might be happy to sort arrays of size 10^4 with insertion sort.
QUESTION
From cppreference, I know that for stable_sort
:
The order of equal elements is guaranteed to be preserved.
I also read this question What is stability in sorting algorithms and why is it important? and understand the concept and basic usage of stability.
Here's how I understand stable sort:
Let's say I have unordered flight departure time and destination.
First, I sort it by time. It would be like
...ANSWER
Answered 2018-Jun-27 at 02:58fox
, red
, and the
are equal for the purposes of the stable_sort
. The cppreference link you have says:
Elements are compared using the given comparison function comp.
So, yes the fox red the
order is fixed for your example, as the stable_sort won't change the relative order of those three (equally short) items.
QUESTION
I have been trying to solve QuickSort and I got thru a scenario where we are selecting pivot element as the middle one.
http://www.java2novice.com/java-sorting-algorithms/quick-sort/
...ANSWER
Answered 2018-Mar-21 at 23:12It is more likely that
(lowerIndex+higherIndex)/2
overflows rather than
lowerIndex+(higherIndex-lowerIndex)/2.
For example for lowerIndex == higherIndex == Integer.MAX_VALUE / 2 + 1
.
QUESTION
The following quote is from "Comparison with other sort algorithms" section from Wikipedia Merge Sort page
On typical modern architectures, efficient quicksort implementations generally outperform mergesort for sorting RAM-based arrays.[citation needed] On the other hand, merge sort is a stable sort and is more efficient at handling slow-to-access sequential media.
My questions:
Why does Quicksort outperform Mergesort when the data to be sorted can all fit into memory? If all data needed are cached or in memory wouldn't it be fast for both Quicksort and Mergesort to access?
Why is Mergesort more efficient at handling slow-to-access sequential data (such as from disk in the case where the data to be sorted can't all fit into memory)?
(move from my comments below to here)In an array
arr
of primitives (data are sequential) of n elements. The pair of elements that has to be read and compared in MergeSort isarr[0]
andarr[n/2]
(happens in the final merge). Now think the pair of elements that has to be read and compared in QuickSort isarr[1]
andarr[n]
(happens in the first partition, assume we swap the randomly chosen pivot with the first element). We know data are read in blocks and load into cache, or disk to memory (correct me if I am wrong) then isn't there a better chance for the needed data gets load together in one block when using MergeSort? It just seems to me MergeSort would always have the upperhand because it is likely comparing elements that are closer together. I know this is False (see graph below) because QuickSort is obviously faster...... I know MergeSort is not in place and requires extra memory and that is likely to slow things down. Other than that what pieces am I missing in my analysis?
images are from Princeton CS MergeSort and QuickSort slides
My Motive:
I want to understand these above concepts because they are one of the main reasons of why mergeSort is preferred when sorting LinkedList,or none sequential data and quickSort is preferred when sorting Array, or sequential data. And why mergeSort is used to sort Object in Java and quickSort is used to sort primitive type in java.
update: Java 7 API actually uses TimSort to sort Object, which is a hybrid of MergeSort and InsertionSort. For primitives Dual-Pivot QuickSort. These changes were implemented starting in Java SE 7. This has to do with the stability of the sorting algorithm. Why does Java's Arrays.sort method use two different sorting algorithms for different types?
Edit:
I will appreciate an answer that addresses the following aspects:
- I know the two sorting algorithms differ in the number of moves, read, and comparisons. If those are that reasons contribute to the behaviors I see listed in my questions (I suspected it) then a thorough explanation of how the steps and process of the sorting algorithm results it having advantages or disadvantages seeking data from disk or memory will be much appreciated.
- Examples are welcome. I learn better with examples.
note: if you are reading @rcgldr's answer. check out our conversation in the chat room it has lots of good explanations and details. https://chat.stackoverflow.com/rooms/161554/discussion-between-rcgldr-and-oliver-koo
...ANSWER
Answered 2017-Dec-23 at 00:01The main difference is that merge sort does more moves, but fewer compares than quick sort. Even in the case of sorting an array of native types, quick sort is only around 15% faster, at least when I've tested it on large arrays of pseudo random 64 bit unsigned integers, which should be quick sort's best case, on my system (Intel 3770K 3.5ghz, Windows 7 Pro 64 bit, Visual Studio 2015, sorting 16 million pseudo random 64 bit unsigned integers, 1.32 seconds for quick sort, 1.55 seconds for merge sort, 1.32/1.55 ~= 0.85, so quick sort was about 15% faster than merge sort). My test was with a quick sort that had no checks to avoid worst case O(n^2) time or O(n) space. As checks are added to quick sort to reduce or prevent worst case behavior (like fall back to heap sort if recursion becomes too deep), the speed advantage decreases to less than 10% (which is the difference I get between VS2015's implementation of std::sort (modified quick sort) versus std::stable_sort (modified merge sort).
If sorting "strings", it's more likely that what is being sorted is an array of pointers (or references) to those strings. This is where merge sort is faster, because the moves involve pointers, while the compares involve a level of indirection and comparison of strings.
The main reason for choosing quick sort over merge sort is not speed, but space requirement. Merge sort normally uses a second array the same size as the original. Quick sort and top down merge sort also need log(n) stack frames for recursion, and for quick sort limiting stack space to log(n) stack frames is done by only recursing on the smaller partition, and looping back to handle the larger partition.
In terms of cache issues, most recent processors have 4 or 8 way associative caches. For merge sort, during a merge, the two input runs will end up in 2 of the cache lines, and the one output run in a 3rd cache line. Quick sort scans the data before doing swaps, so the scanned data will be in cache, although in separate lines if the two elements being compared / swapped are located far enough from each other.
For an external sort, some variation of bottom up merge sort is used. This because merge sort merge operations are sequential (the only random access occurs when starting up a new pair of runs), which is fast in the case of hard drives, or in legacy times, tape drives (a minimum of 3 tapes drives is needed). Each read or write can be for very large blocks of data, reducing average access time per element in the case of a hard drive, since a large number of elements are read or written at a time with each I/O.
It should also be noted that most merge sorts in libraries are also some variation of bottom up merge sort. Top down merge sort is mostly a teaching environment implementation.
If sorting an array of native types on a processor with 16 registers, such as an X86 in 64 bit mode, 8 of the registers used as start + end pointers (or references) for 4 runs, then a 4-way merge sort is often about the same or a bit faster than quick sort, assuming a compiler optimizes the pointers or references to be register based. It's a similar trade off, like quick sort, 4-way merge sort does more compares (1.5 x compares), but fewer moves (0.5 x moves) than traditional 2-way merge sort.
It should be noted that these sorts are cpu bound, not memory bound. I made a multi-threaded version of a bottom up merge sort, and in the case of using 4 threads, the sort was 3 times faster. Link to Windows example code using 4 threads:
https://codereview.stackexchange.com/questions/148025/multithreaded-bottom-up-merge-sort
QUESTION
I'm reading through the khan academy course on algorithms. I'm at https://www.khanacademy.org/computing/computer-science/algorithms/sorting-algorithms/p/project-selection-sort-visualizer .
The code is working and I can get it to print to the console but I need to be able to print to the canvas.
I've tried :
...ANSWER
Answered 2017-Jun-06 at 15:10You're still using "println", you need to use 'text' function.
http://processingjs.org/reference/text_/ is the reference on how to use this.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install sorting-algorithms
You can use sorting-algorithms like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the sorting-algorithms component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page