forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Postman sort implemented in Dart (jainaman224#3001)
- Loading branch information
1 parent
a87620b
commit 0d5be80
Showing
1 changed file
with
99 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,99 @@ | ||
// Importing required libraries | ||
import 'dart:io'; | ||
import 'dart:math'; | ||
|
||
// Declaring required variables | ||
int temp, max, count, maxdigits = 0, c = 0; | ||
int t1, t, i, j; | ||
List array, array1; | ||
int n = 1; | ||
|
||
// Method to arrange numbers that have same base | ||
void arrange(int k, int n){ | ||
for(int i = k; i < n - 1; i ++){ | ||
for(int j = i + 1; j < n; j ++){ | ||
if(array1[i] > array1[j]){ | ||
temp = array1[i]; | ||
array1[i] = array1[j]; | ||
array1[j] = temp; | ||
temp = array[i]%10; | ||
array[i] = array[j]%10; | ||
array[j] = temp; | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
void main(){ | ||
|
||
// Input of the size of the array | ||
print("Enter the number of elements in the array:"); | ||
var input = stdin.readLineSync(); | ||
count = int.parse(input); | ||
|
||
// Input of array elements | ||
print("Enter the array elements:"); | ||
input = stdin.readLineSync(); | ||
var lis = input.split(' '); | ||
array = lis.map(int.parse).toList(); | ||
array1 = lis.map(int.parse).toList(); | ||
|
||
// Finding the maximum possible length of the numbers in the array | ||
for(i = 0; i < count; i ++){ | ||
t = array[i]; | ||
c = (log(array[i])/log(10)).floor(); | ||
if(c > maxdigits){ | ||
maxdigits = c; | ||
} | ||
} | ||
|
||
for(i = 0; i < maxdigits - 1; i ++){ | ||
n = n * 10; | ||
} | ||
|
||
for(i = 0; i < count; i ++){ | ||
max = (array[i]/n).floor(); | ||
t = i; | ||
for(j = i + 1; j < count; j ++){ | ||
if(max > (array[j]/n).floor()){ | ||
max = (array[j]/n).floor(); | ||
t = j; | ||
} | ||
} | ||
|
||
// Swapping | ||
temp = array1[t]; | ||
array1[t] = array1[i]; | ||
array1[i] = temp; | ||
temp = array[t]; | ||
array[t] = array[i]; | ||
array[i] = temp; | ||
} | ||
|
||
while(n >= 1){ | ||
for(i = 0; i < count;){ | ||
t1 = (array[i]/n).floor(); | ||
for(j = i + 1; j < count && t1 == (array[j]/n).floor(); j ++){ | ||
arrange(i, j); | ||
} | ||
i = j; | ||
} | ||
n = (n / 10).floor(); | ||
} | ||
|
||
print("The sorted array is:"); | ||
print(array.join(" ")); | ||
} | ||
|
||
/** | ||
* Sample Input and Output | ||
* ---------------------------- | ||
* Enter the number of elements in the array: | ||
* 10 | ||
* Enter the array elements: | ||
* 10 9 8 7 6 5 4 3 2 1 | ||
* The sorted array is: | ||
* 1 2 3 4 5 6 7 8 9 10 | ||
* | ||
*/ |