-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Postman Sort in JavaScript (#2729)
Modified Modified
- Loading branch information
1 parent
7f0ec41
commit 9dc9e66
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* JavaScript Implementation for Postman Sort Algorithm | ||
*/ | ||
|
||
let temp, max, count, maxdigits = 0, c = 0; | ||
let t1, t2, k, t, n = 1, p, s; | ||
|
||
/* Function to arrange the of sequence having same base */ | ||
function arrange(p, s) { | ||
for(var i = p; i < s - 1; i++) { | ||
for(var j = i + 1; j < s; j++) { | ||
if(arr1[i] > arr1[j]) { | ||
// swap array1[i] and array1[j] | ||
swap (arr1[i], arr1[j]); | ||
|
||
temp = arr[i] % 10; | ||
arr[i] = arr[j] % 10; | ||
arr[j] = temp; | ||
} | ||
} | ||
} | ||
} | ||
|
||
count = 10; | ||
console.log("The size of the Array : " + count); | ||
let arr = [70, 80, 20, 50, 10, 60, 40, 100, 90, 30]; | ||
console.log("The Unsorted Array is : "); | ||
console.log(arr); | ||
|
||
let arr1 = new Array(); | ||
for(let i = 0; i < count; i++) { | ||
// first element in t | ||
t = arr[i]; | ||
// Calculating the Number of digits using logarithmic | ||
c = Math.trunc(Math.log(arr[i] / Math.log(10))); | ||
// number of digits in a each number | ||
if(maxdigits < c) | ||
maxdigits = c; | ||
} | ||
|
||
while(--maxdigits) | ||
n = n * 10; | ||
|
||
for(var i = 0; i < count; i++) { | ||
// MSB - Dividing by particular base | ||
max = arr[i] / n; | ||
t = i; | ||
for(var j = i + 1; j < count; j++) { | ||
if(max > arr[j] / n) { | ||
max = arr[j] / n; | ||
t = j; | ||
} | ||
} | ||
temp = arr1[t]; | ||
arr1[t] = arr1[i]; | ||
arr1[i] = temp; | ||
temp = arr[t]; | ||
arr[t] = arr[i]; | ||
arr[i] = temp; | ||
} | ||
|
||
function swap(x, y) { | ||
var t = x; | ||
x = y; | ||
y = t; | ||
} | ||
|
||
while (n >= 1) { | ||
for(var i = 0; i < count;) { | ||
t1 = arr[i] / n; | ||
for(j = i + 1; t1 == (arr[j] / n); j++); | ||
arrange(i, j); | ||
i = j; | ||
} | ||
n = n / 10; | ||
} | ||
|
||
console.log("Sorted Array is : "); | ||
console.log(arr); | ||
|
||
/* | ||
INPUT : | ||
The size of the Array : 10 | ||
The Unsorted Array is : [70, 80, 20, 50, 10, 60, 40, 100, 90, 30] | ||
OUTPUT : | ||
Sorted Array is : [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] | ||
*/ |