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

Commit a89adde

Browse files
authored
Added Sorting Algorithms
1 parent d4756f2 commit a89adde

File tree

1 file changed

+218
-0
lines changed

1 file changed

+218
-0
lines changed

‎README.md‎

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
11. [Iterables and iterators](#iterables-and-iterators)
1414
12. [Generators](#generators)
1515
13. [Coding Problems](#coding-problems)
16+
14. [Sorting Algorithms](#sorting-algorithms)
1617

1718
## Variable scope
1819

@@ -1270,6 +1271,223 @@ genObj1.return('Result')
12701271
Exiting
12711272
{ value: 'Result', done: true }
12721273
```
1274+
## Sorting Algorithms
1275+
Sorting is considered to be an important concept in many programming languages as it helps us locate elements in a faster and easier manner.
1276+
### Bubble Sort
1277+
How it works:
1278+
1. you compare the first item with the second. If the first item is bigger than the second item. you swap them so that the bigger one stays in the second position.
1279+
1280+
2. And then compare second with third item. if second item is bigger than the third, we swap them. otherwise, they stayed in their position. Hence, the biggest among first three is in the third position.
1281+
1282+
3. We keep doing it. until we hit the last element of the array. In that way we bubble up the biggest item of the array to the right most position of the array.
1283+
1284+
4. Look at the inner loop in the code below.
1285+
1286+
5. We repeat this process, starting from the last item of the array. look at the outer loop in the code below. We do this way, so that after finishing the first inner loop, the biggest one will be in the last item of the array.
1287+
1288+
6. And then we move backward inside the outer loop.
1289+
```
1290+
function bubbleSort(arr){
1291+
var len = arr.length;
1292+
for (var i = len-1; i >= 0; i--){
1293+
for(var j = 1; j<=i; j++){
1294+
if(arr[j-1] > arr[j]){
1295+
var temp = arr[j-1];
1296+
arr[j-1] = arr[j];
1297+
arr[j] = temp;
1298+
}
1299+
}
1300+
}
1301+
return arr;
1302+
}
1303+
bubbleSort([7,5,2,4,3,9]); //[2, 3, 4, 5, 7, 9]
1304+
bubbleSort([9,7,5,4,3,1]); //[1, 3, 4, 5, 7, 9]
1305+
bubbleSort([1,2,3,4,5,6]); //[1, 2, 3, 4, 5, 6]
1306+
```
1307+
### Selection Sort
1308+
This is very simple. Go through the array, find the index of the lowest element swap the lowest element with the first element. Hence first element is the lowest element in the array.
1309+
1310+
Now go through the rest of the array (excluding the first element) and find the index of the lowest and swap it with the second element.
1311+
1312+
thats how it continues to select (find out) the lowest element of the array and putting it on the left until it hits the last element.
1313+
```
1314+
function selectionSort(arr){
1315+
var minIdx, temp,
1316+
len = arr.length;
1317+
for(var i = 0; i < len; i++){
1318+
minIdx = i;
1319+
for(var j = i+1; j < len; j++){
1320+
if(arr[j] < arr[minIdx]){
1321+
minIdx = j;
1322+
}
1323+
}
1324+
temp = arr[i];
1325+
arr[i] = arr[minIdx];
1326+
arr[minIdx] = temp;
1327+
}
1328+
return arr;
1329+
}
1330+
selectionSort([7,5,2,4,3,9]); //[2, 3, 4, 5, 7, 9]
1331+
```
1332+
### Insertion sort
1333+
It starts with the second element. Pick the second element to be inserted and then compare to the previous element. If the first one is bigger, move the first one to second position and second one at first.
1334+
1335+
Now first and second item is sorted.
1336+
1337+
Then, pick the third element and check whether the second element is bigger than the third. keep going similar way until you hit the first element or a element smaller than the element you are comparing with. When you get an item smaller than the picked item, you insert it.
1338+
```
1339+
function insertionSort(arr) {
1340+
var i, len = arr.length, el, j;
1341+
1342+
for(i = 1; i < len; i++){
1343+
el = arr[i];
1344+
j = i;
1345+
1346+
while(j > 0 && arr[j-1] > toInsert){
1347+
arr[j] = arr[j-1];
1348+
j--;
1349+
}
1350+
arr[j] = el;
1351+
}
1352+
return arr;
1353+
}
1354+
```
1355+
### Merge Sort
1356+
Merge sort has two parts. Main part does divide or breaks down and second part is merging/combining parts. At the time of combining, parts are combined together.
1357+
1358+
Divide: the first function named as mergeSort is actually a divide function. where an array is divided into two.
1359+
1360+
merge: this is just merging two sorted array. Just be careful this two array could be in different size
1361+
1362+
```
1363+
function mergeSort(arr){
1364+
var len = arr.length;
1365+
if(len <2)
1366+
return arr;
1367+
var mid = Math.floor(len/2),
1368+
left = arr.slice(0,mid),
1369+
right =arr.slice(mid);
1370+
//send left and right to the mergeSort to broke it down into pieces
1371+
//then merge those
1372+
return merge(mergeSort(left),mergeSort(right));
1373+
}
1374+
1375+
function merge(left, right){
1376+
var result = [],
1377+
lLen = left.length,
1378+
rLen = right.length,
1379+
l = 0,
1380+
r = 0;
1381+
while(l < lLen && r < rLen){
1382+
if(left[l] < right[r]){
1383+
result.push(left[l++]);
1384+
}
1385+
else{
1386+
result.push(right[r++]);
1387+
}
1388+
}
1389+
//remaining part needs to be addred to the result
1390+
return result.concat(left.slice(l)).concat(right.slice(r));
1391+
}
1392+
```
1393+
### Quick sort
1394+
Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively sort the sub-arrays
1395+
1396+
The steps are:
1397+
1398+
1. Pick an element, called a pivot, from the array.
1399+
2. Partitioning: reorder the array so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation.
1400+
3. Recursively apply the above steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values.
1401+
```
1402+
function quickSort(arr, left, right){
1403+
var len = arr.length,
1404+
pivot,
1405+
partitionIndex;
1406+
1407+
1408+
if(left < right){
1409+
pivot = right;
1410+
partitionIndex = partition(arr, pivot, left, right);
1411+
1412+
//sort left and right
1413+
quickSort(arr, left, partitionIndex - 1);
1414+
quickSort(arr, partitionIndex + 1, right);
1415+
}
1416+
return arr;
1417+
}
1418+
1419+
function partition(arr, pivot, left, right){
1420+
var pivotValue = arr[pivot],
1421+
partitionIndex = left;
1422+
1423+
for(var i = left; i < right; i++){
1424+
if(arr[i] < pivotValue){
1425+
swap(arr, i, partitionIndex);
1426+
partitionIndex++;
1427+
}
1428+
}
1429+
swap(arr, right, partitionIndex);
1430+
return partitionIndex;
1431+
}
1432+
1433+
function swap(arr, i, j){
1434+
var temp = arr[i];
1435+
arr[i] = arr[j];
1436+
arr[j] = temp;
1437+
}
1438+
```
1439+
### Heap sort
1440+
Heapsort is a comparison-based sorting algorithm. Heapsort can be thought of as an improved selection sort: like that algorithm, it divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. The improvement consists of the use of a heap data structure rather than a linear-time search to find the maximum.
1441+
1442+
```
1443+
function heapSort(arr){
1444+
var len = arr.length,
1445+
end = len-1;
1446+
1447+
heapify(arr, len);
1448+
1449+
while(end > 0){
1450+
swap(arr, end--, 0);
1451+
siftDown(arr, 0, end);
1452+
}
1453+
return arr;
1454+
}
1455+
1456+
function heapify(arr, len){
1457+
// break the array into root + two sides, to create tree (heap)
1458+
var mid = Math.floor((len-2)/2);
1459+
while(mid >= 0){
1460+
siftDown(arr, mid--, len-1);
1461+
}
1462+
}
1463+
function siftDown(arr, start, end){
1464+
var root = start,
1465+
child = root*2 + 1,
1466+
toSwap = root;
1467+
while(child <= end){
1468+
if(arr[toSwap] < arr[child]){
1469+
swap(arr, toSwap, child);
1470+
}
1471+
if(child+1 <= end && arr[toSwap] < arr[child+1]){
1472+
swap(arr, toSwap, child+1)
1473+
}
1474+
if(toSwap != root){
1475+
swap(arr, root, toSwap);
1476+
root = toSwap;
1477+
}
1478+
else{
1479+
return;
1480+
}
1481+
toSwap = root;
1482+
child = root*2+1
1483+
}
1484+
}
1485+
function swap(arr, i, j){
1486+
var temp = arr[i];
1487+
arr[i] = arr[j];
1488+
arr[j] = temp;
1489+
}
1490+
```
12731491
## Coding Problems
12741492
### Check prime number
12751493
A prime number is only divisible by itself and 1.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /