-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from AbhiC7721/master
Sorting a 0 1 2 array without using any sorting algorithm
- Loading branch information
Showing
1 changed file
with
35 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,35 @@ | ||
'''This is a program to sort the elements | ||
of a 0, 1, 2 without using any sorting algorithm | ||
''' | ||
|
||
'''This algrithm divides an array into 4 parts: | ||
first part contains 0, second part contains 1, | ||
third part contains unknown elements and the | ||
fourth part contains 2 | ||
First part -> 0 to l ... contains 0s | ||
Second part -> l to mid ... contains 1s | ||
Third part -> mid to h ... contains unknown elements | ||
Fourth part -> h to size ... contains 2s | ||
''' | ||
def sortarr(a, size): | ||
l = 0 | ||
h = size-1 | ||
mid = 0 | ||
while mid<=h: | ||
if a[mid] == 0: | ||
a[l], a[mid] = a[mid], a[l] | ||
l = l+1 | ||
mid = mid +1 | ||
elif a[mid] == 1: | ||
mid = mid + 1 | ||
else: | ||
a[mid], a[h] = a[h], a[mid] | ||
h = h-1 | ||
return a | ||
|
||
print("This is a program to sort a 012 array") | ||
print("Enter the elements of the array: ") | ||
arr = list(map(int, input().split())) | ||
arr = sortarr(arr, len(arr)) | ||
print("Array after sorting or segregation: ") | ||
print(*arr) |