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

Commit 0cb151d

Browse files
Addition of sorting algorithms and it's implementation.
1 parent ff58956 commit 0cb151d

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

‎Sort/InsertionSort.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
__author__ = 'Sanjay'
2+
3+
def insertionSort(alist):
4+
for index in range(1,len(alist)):
5+
6+
currentvalue = alist[index]
7+
position = index
8+
9+
while position>0 and alist[position-1]>currentvalue:
10+
alist[position]=alist[position-1]
11+
position = position-1
12+
13+
alist[position]=currentvalue
14+
15+
if __name__ == '__ main__':
16+
17+
18+
alist = [54,26,93,17,77,31,44,55,20]
19+
insertionSort(alist)
20+
print(alist)

‎Sort/MergeSort.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
__author__ = 'Sanjay'
2+
3+
# Merge sort is a very efficient sorting algorithm.
4+
# It’s based on the divide-and-conquer approach, a powerful algorithmic technique used to solve complex problems.
5+
6+
def mergeSort(alist):
7+
print("Splitting ",alist)
8+
if len(alist)>1:
9+
mid = len(alist)//2
10+
lefthalf = alist[:mid]
11+
righthalf = alist[mid:]
12+
13+
mergeSort(lefthalf)
14+
mergeSort(righthalf)
15+
16+
i=0
17+
j=0
18+
k=0
19+
while i < len(lefthalf) and j < len(righthalf):
20+
if lefthalf[i] < righthalf[j]:
21+
alist[k]=lefthalf[i]
22+
i=i+1
23+
else:
24+
alist[k]=righthalf[j]
25+
j=j+1
26+
k=k+1
27+
28+
while i < len(lefthalf):
29+
alist[k]=lefthalf[i]
30+
i=i+1
31+
k=k+1
32+
33+
while j < len(righthalf):
34+
alist[k]=righthalf[j]
35+
j=j+1
36+
k=k+1
37+
print("Merging ",alist)
38+
39+
if __name__ == '__main__':
40+
41+
alist = [54,26,93,17,77,31,44,55,20]
42+
mergeSort(alist)
43+
print(alist)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /