Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit25d21eb

Browse files
committed
explanations on quick sort algo in its own README.md file
1 parentcd7de62 commit25d21eb

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

‎Sort/QuickSort/README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,40 @@
11
#Quick Sort
22

3+
The algorithm for Quicksort is:
34

4-
###My Personal Blog Post -[https://rohan-paul.github.io/javascript/2018/01/11/Quick-Sort_Algorithm-in-JavaScript/](https://rohan-paul.github.io/javascript/2018/01/11/Quick-Sort_Algorithm-in-JavaScript/)
5+
1. Pick a pivot element that divides the list into two sublists.
6+
7+
2. Start a pointer, to reorder the list so that all elements less than the pivot element are placed before the pivot and all elements greater than the pivot are placed after it.
8+
9+
So, e.g. if I choose the pivot to be at 0-index element, I can start that pointer from index-1 element array. However, under this algorithm (selecting the first item as a pivot), I would get worst-case performance on already sorted arrays.
10+
11+
3. After this partitioning, the pivot is in its final position. This is called the partition operation.
12+
13+
4. Repeat steps 1 and 2 on both the list with smaller elements and the list of larger
14+
elements.
15+
16+
5. Quicksort is a divide and conquer algorithm in the style of merge sort. The basic idea is to find a “pivot” item in the array to compare all other items against, then shift items such that all of the items before the pivot are less than the pivot value and all the items after the pivot are greater than the pivot value. After that, recursively perform the same operation on the items before and after the pivot.
17+
18+
Animated visualization of the quicksort algorithm.
19+
The horizontal lines are pivot values.
20+
21+
![Quicksort](https://upload.wikimedia.org/wikipedia/commons/f/fe/Quicksort.gif)
22+
23+
##Complexity
24+
25+
| Name| Best| Average| Worst| Memory| Stable| Comments|
26+
| ---------------------| :-------------:| :-----------------:| :-----------------:| :-------:| :-------:| :--------|
27+
|**Quick sort**| n&nbsp;log(n)| n&nbsp;log(n)| n<sup>2</sup>| log(n)| No||
28+
29+
##References
30+
31+
-[Wikipedia](https://en.wikipedia.org/wiki/Quicksort)
32+
33+
34+
For arrays that are nearly or completely sorted, quicksort operates at its worst. In other words, the average runtime for a sorted or a nearly-sorted list is quicksort’s worst case runtime:**O(n²)**.
35+
This is particularly important to remember if we know that we’ll be dealing with data that’s mostly sorted. In those situations, we want to stay away from quicksort, since it’s going to take a really long time to run.
36+
37+
Node, for example, uses quicksort under the hood of its Array.sort function; however, for shorter arrays (in node’s case, those with a length less than or equal to 10), it uses insertion sort! You can see the logic they use to determine that deep within[v8’s source code](https://github.com/v8/v8/blob/master/src/js/array.js#L695).
38+
39+
40+
###My Personal Blog Post on Quick Sort -[https://rohan-paul.github.io/javascript/2018/01/11/Quick-Sort_Algorithm-in-JavaScript/](https://rohan-paul.github.io/javascript/2018/01/11/Quick-Sort_Algorithm-in-JavaScript/)

‎Sort/QuickSort/quick-sort-basic-Recursive-FUTURE-REFERENCE.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
/*The algorithm for Quicksort is:
2+
23
1. Pick a pivot element that divides the list into two sublists.
34
45
2. Reorder the list so that all elements less than the pivot element are placed before
56
the pivot and all elements greater than the pivot are placed after it.
67
8+
3. After this partitioning, the pivot is in its final position. This is called the partition operation.
9+
710
3. Repeat steps 1 and 2 on both the list with smaller elements and the list of larger
811
elements.
912

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp