-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sorting of array elements.
- Loading branch information
Manish Kumar
committed
Aug 22, 2018
1 parent
8949fdb
commit 90b3955
Showing
4 changed files
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,20 @@ | ||
#Bubble Sorting by Master-Fury | ||
#Worst and Average Case Time Complexity: O(n*n) | ||
|
||
def Bubble_Sort(arr): | ||
l=len(arr) | ||
for i in range(l): | ||
for j in range(0,l-i-1): | ||
if(arr[j]>arr[j+1]): | ||
arr[j],arr[j+1]=arr[j+1],arr[j] | ||
return (arr) | ||
|
||
|
||
#Driver Code | ||
A=[1,34,21,345,765,44,67,32] #Your array | ||
res=Bubble_Sort(A) | ||
print(res) | ||
|
||
|
||
#DESCRIPTION | ||
#Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. |
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,23 @@ | ||
#Insertion Sort by Master-Fury | ||
#Time Complexity: O(n*n) | ||
|
||
def Insertion_Sort(arr): | ||
for i in range(1,len(arr)): | ||
pos=arr[i] | ||
j=i-1 | ||
while j>=0 and pos<arr[j]: | ||
arr[j+1]=arr[j] | ||
j-=1 | ||
arr[j+1]=pos | ||
|
||
#Driver Code | ||
arr=[43,25,44,78,453,897,6,54] #Your array | ||
Insertion_Sort(arr) | ||
print(arr) | ||
|
||
|
||
#Description | ||
#We can use binary search to reduce the number of comparisons in normal insertion sort. | ||
#Binary Insertion Sort find use binary search to find the proper location to insert the selected item at each iteration. | ||
# Insertion sort is used when number of elements is small. | ||
#It can also be useful when input array is almost sorted, only few elements are misplaced in complete big array. |
23 changes: 23 additions & 0 deletions
23
Arrays-Sorting/src/Recursive_Bubble_Sort/Recursive_Bubble_Sort.py
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,23 @@ | ||
#Recursive Bubble Sort by Master Fury | ||
|
||
|
||
def Bubble_sort_rec(arr,l): | ||
if l==1: | ||
return | ||
for i in range(l-1): | ||
if arr[i]>arr[i+1]: | ||
arr[i],arr[i+1]=arr[i+1],arr[i] | ||
Bubble_sort_rec(arr,l-1) #Recursion | ||
return arr | ||
|
||
#Driver Code | ||
arr=[34,76,45,342,54,6,788,23] #Your array | ||
l=len(arr) | ||
Bubble_sort_rec(arr,l) | ||
print(arr) | ||
|
||
|
||
|
||
|
||
#Recursive Bubble Sort has no performance/implementation advantages, | ||
#but can be a good question to check one’s understanding of Bubble Sort and recursion. |
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,22 @@ | ||
#Selection Sort by Master-Fury | ||
#Time Complexity:O(n^2) | ||
|
||
def Selection_Sort(arr): | ||
for i in range(len(arr)): | ||
min_index=i | ||
for j in range(i+1, len(arr)): | ||
if arr[min_index]>arr[j]: | ||
min_index=j | ||
arr[i],arr[min_index]=arr[min_index],arr[i] | ||
return arr; | ||
|
||
#Driver Code | ||
A=[1,33,56,21,3,78,54] #Your array | ||
res=Selection_Sort(A) | ||
for i in range(len(res)): | ||
print(res[i]) | ||
|
||
|
||
#Description | ||
#The selection sort algorithm sorts an array by repeatedly finding the minimum element | ||
#(considering ascending order) from unsorted part and putting it at the beginning. |