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

Commitcd7de62

Browse files
committed
setSort algo of arrays
1 parent63a11fe commitcd7de62

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

‎Sort/BubbleSort/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ bubble-sort should not be used for larger arrays, can be used for smaller ones f
1111
| ---------------------| :-------------:| :-----------------:| :-----------------:| :-------:| :-------:| :--------|
1212
|**Bubble sort**| n| n<sup>2</sup>| n<sup>2</sup>| 1| Yes||
1313

14+
The worst case scenario of this algorithm is quadratic — O(n²) — because it’s possible that the data will be the exact opposite of ordered. Best case scenario is linear — O(n) — because even if it’s entirely ordered, we have to check through each set of numbers.
1415

1516
##MOST IMPORTANT POINT OF BUBBLE SORT (And base of my solution SOLUTION-3 ) :-
1617

‎Sort/BubbleSort/bubble-sort-basic-FUTURE-REFERENCE.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ bubbleSortBasicDescending = (arr) => {
6969
7070
So, here, I only do the loops and swaps for the cases when I find a mis-placed element, i.e. larger-element placed before smaller in an ascending sort
7171
72-
Remember the do-while syntax - the first do will execute anyway, but then the next while loop will ONLY execute if the condition of thw while is satisfied. In this case below, only after I find a mis-placed pair of elements, I flip 'swapped' to true. And only then while(swapped) gets satisfied > and only then the next while loop gets executed.
72+
Remember the do-while syntax - Unlike the while loop, the do-while loop always executes the body at least one before it evaluates the expression.
73+
So the first do will execute anyway, but then the next while loop will ONLY execute if the condition of thw while is satisfied. In this case below, only after I find a mis-placed pair of elements, I flip 'swapped' to true. And only then while(swapped) gets satisfied > and only then the next while loop gets executed.
7374
7475
So, here, I traverse the array only once. And only the first time, I find a mis-placed element, that is I do an actual swap, do I make that variable 'swap' to be true.
7576

‎Sort/setSort/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#Set Sort
2+
3+
It’s really really fast, and it’s far quicker than the quicksort algorithm, which is considered as the fastest sorting algorithm in practice.
4+
5+
However, the simplest version of it only works with integers, and I’ve to know the first and the last element from that set and also every element is unique
6+
7+
1. First Pass
8+
First we have an unsorted array, but we know the minimum and maximum of the set.
9+
10+
On the first pass initialize an empty array (lets call it newArray ) with as many elements, as they are between the first and the last element of the unsorted array – for a set between 1 and 1000 – that will be an array with 1000 elements – each of which will be a zero in the beginning.
11+
12+
Than loop trough the the given unsorted array and for every element in that unsorted – put a 1 on it’s index position.
13+
14+
That is, if the unsorted array is[ 3, 2, 5], for the newArr[3] will have 1 as its element, and newArr[2] is 1 and newArr[5] is equal to 1
15+
16+
Now from the newArray just return those elements whose value is 1.

‎Sort/setSort/setSort.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
setSort_1000=arr=>{
2+
3+
letnewArr=newArray(1000).fill(0)
4+
5+
letsortedArr=[];
6+
7+
for(leti=0;i<arr.length;i++){
8+
newArr[arr[i]]=1// this 1 can be any other arbitrary number. Becuase its just a hook with which I will get the key in the next step
9+
}
10+
11+
for(leti=0;i<newArr.length;i++){
12+
if(1===newArr[i]){
13+
sortedArr.push(i)
14+
}
15+
}
16+
returnsortedArr;
17+
}
18+
19+
letmyArr=[34,203,3,746,200,984,198,764];
20+
21+
console.log(setSort_1000(myArr));// => [ 3, 34, 198, 200, 203, 746, 764, 984 ]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp