-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Python Program implementation | ||
# of binary insertion sort | ||
|
||
|
||
def binary_search(arr, val, start, end): | ||
# we need to distinugish whether we | ||
# should insert before or after the | ||
# left boundary. imagine [0] is the last | ||
# step of the binary search and we need | ||
# to decide where to insert -1 | ||
if start == end: | ||
if arr[start] > val: | ||
return start | ||
else: | ||
return start + 1 | ||
|
||
# this occurs if we are moving | ||
# beyond left's boundary meaning | ||
# the left boundary is the least | ||
# position to find a number greater than val | ||
if start > end: | ||
return start | ||
|
||
mid = (start + end) // 2 | ||
if arr[mid] < val: | ||
return binary_search(arr, val, mid + 1, end) | ||
elif arr[mid] > val: | ||
return binary_search(arr, val, start, mid - 1) | ||
else: | ||
return mid | ||
|
||
|
||
def insertion_sort(arr): | ||
for i in range(1, len(arr)): | ||
val = arr[i] | ||
j = binary_search(arr, val, 0, i - 1) | ||
arr = arr[:j] + [val] + arr[j:i] + arr[i + 1:] | ||
return arr | ||
|
||
|
||
print("Sorted array:") | ||
print(insertion_sort([37, 23, 0, 31, 22, 17, 12, 72, 31, 46, 100, 88, 54])) | ||
|
||
# Code contributed by Mohit Gupta_OMG |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Iterative Binary Search Function | ||
# It returns index of x in given array arr if present, | ||
# else returns -1 | ||
def binary_search(arr, x): | ||
low = 0 | ||
high = len(arr) - 1 | ||
mid = 0 | ||
|
||
while low <= high: | ||
|
||
mid = (high + low) // 2 | ||
|
||
# If x is greater, ignore left half | ||
if arr[mid] < x: | ||
low = mid + 1 | ||
|
||
# If x is smaller, ignore right half | ||
elif arr[mid] > x: | ||
high = mid - 1 | ||
|
||
# means x is present at mid | ||
else: | ||
return mid | ||
|
||
# If we reach here, then the element was not present | ||
return -1 | ||
|
||
|
||
# Test array | ||
arr = [2, 3, 4, 10, 40] | ||
x = 10 | ||
|
||
# Function call | ||
result = binary_search(arr, x) | ||
|
||
if result != -1: | ||
print("Element is present at index", str(result)) | ||
else: | ||
print("Element is not present in array") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Python 3 program for recursive binary search. | ||
# Modifications needed for the older Python 2 are found in comments. | ||
|
||
# Returns index of x in arr if present, else -1 | ||
def binary_search(arr, low, high, x): | ||
# Check base case | ||
if high >= low: | ||
|
||
mid = (high + low) // 2 | ||
|
||
# If element is present at the middle itself | ||
if arr[mid] == x: | ||
return mid | ||
|
||
# If element is smaller than mid, then it can only | ||
# be present in left subarray | ||
elif arr[mid] > x: | ||
return binary_search(arr, low, mid - 1, x) | ||
|
||
# Else the element can only be present in right subarray | ||
else: | ||
return binary_search(arr, mid + 1, high, x) | ||
|
||
else: | ||
# Element is not present in the array | ||
return -1 | ||
|
||
|
||
# Test array | ||
arr = [2, 3, 4, 10, 40] | ||
x = 10 | ||
|
||
# Function call | ||
result = binary_search(arr, 0, len(arr) - 1, x) | ||
|
||
if result != -1: | ||
print("Element is present at index", str(result)) | ||
else: | ||
print("Element is not present in array") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Python program for Bitonic Sort. Note that this program | ||
# works only when size of input is a power of 2. | ||
|
||
# The parameter dir indicates the sorting direction, ASCENDING | ||
# or DESCENDING; if (a[i] > a[j]) agrees with the direction, | ||
# then a[i] and a[j] are interchanged.*/ | ||
|
||
|
||
def compAndSwap(a, i, j, dire): | ||
if (dire == 1 and a[i] > a[j]) or (dire == 0 and a[i] > a[j]): | ||
a[i], a[j] = a[j], a[i] | ||
|
||
# It recursively sorts a bitonic sequence in ascending order, | ||
# if dir = 1, and in descending order otherwise (means dir=0). | ||
# The sequence to be sorted starts at index position low, | ||
# the parameter cnt is the number of elements to be sorted. | ||
|
||
|
||
def bitonicMerge(a, low, cnt, dire): | ||
if cnt > 1: | ||
k = cnt//2 | ||
for i in range(low, low+k): | ||
compAndSwap(a, i, i+k, dire) | ||
bitonicMerge(a, low, k, dire) | ||
bitonicMerge(a, low+k, k, dire) | ||
|
||
# This function first produces a bitonic sequence by recursively | ||
# sorting its two halves in opposite sorting orders, and then | ||
# calls bitonicMerge to make them in the same order | ||
|
||
|
||
def bitonicSort(a, low, cnt, dire): | ||
if cnt > 1: | ||
k = cnt//2 | ||
bitonicSort(a, low, k, 1) | ||
bitonicSort(a, low+k, k, 0) | ||
bitonicMerge(a, low, cnt, dire) | ||
|
||
# Caller of bitonicSort for sorting the entire array of length N | ||
# in ASCENDING order | ||
|
||
|
||
def sort(a, N, up): | ||
bitonicSort(a, 0, N, up) | ||
|
||
|
||
# Driver code to test above | ||
a = [3, 7, 4, 8, 6, 2, 1, 5] | ||
n = len(a) | ||
up = 1 | ||
|
||
sort(a, n, up) | ||
print("Sorted array is") | ||
for i in range(n): | ||
print("%d" % a[i], end=" ") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Python program for implementation of Bogo Sort | ||
import random | ||
|
||
# Sorts array a[0..n-1] using Bogo sort | ||
def bogoSort(a): | ||
n = len(a) | ||
while (is_sorted(a)== False): | ||
shuffle(a) | ||
|
||
# To check if array is sorted or not | ||
def is_sorted(a): | ||
n = len(a) | ||
for i in range(0, n-1): | ||
if (a[i] > a[i+1] ): | ||
return False | ||
return True | ||
|
||
# To generate permutation of the array | ||
def shuffle(a): | ||
n = len(a) | ||
for i in range (0,n): | ||
r = random.randint(0,n-1) | ||
a[i], a[r] = a[r], a[i] | ||
|
||
# Driver code to test above | ||
a = [3, 2, 4, 1, 0, 5] | ||
bogoSort(a) | ||
print("Sorted array :") | ||
for i in range(len(a)): | ||
print ("%d" %a[i]), |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Python program for implementation of Bubble Sort | ||
|
||
def bubbleSort(arr): | ||
n = len(arr) | ||
# optimize code, so if the array is already sorted, it doesn't need | ||
# to go through the entire process | ||
swapped = False | ||
# Traverse through all array elements | ||
for i in range(n - 1): | ||
# range(n) also work but outer loop will | ||
# repeat one time more than needed. | ||
# Last i elements are already in place | ||
for j in range(0, n - i - 1): | ||
|
||
# traverse the array from 0 to n-i-1 | ||
# Swap if the element found is greater | ||
# than the next element | ||
if arr[j] > arr[j + 1]: | ||
swapped = True | ||
arr[j], arr[j + 1] = arr[j + 1], arr[j] | ||
|
||
if not swapped: | ||
# if we haven't needed to make a single swap, we | ||
# can just exit the main loop. | ||
return | ||
|
||
|
||
# Driver code to test above | ||
arr = [64, 34, 25, 12, 22, 11, 90] | ||
|
||
bubbleSort(arr) | ||
|
||
print("Sorted array is:") | ||
for i in range(len(arr)): | ||
print("% d" % arr[i], end=" ") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Python program for implementation of Cocktail Sort | ||
|
||
def cocktailSort(a): | ||
n = len(a) | ||
swapped = True | ||
start = 0 | ||
end = n-1 | ||
while (swapped==True): | ||
|
||
# reset the swapped flag on entering the loop, | ||
# because it might be true from a previous | ||
# iteration. | ||
swapped = False | ||
|
||
# loop from left to right same as the bubble | ||
# sort | ||
for i in range (start, end): | ||
if (a[i] > a[i+1]) : | ||
a[i], a[i+1]= a[i+1], a[i] | ||
swapped=True | ||
|
||
# if nothing moved, then array is sorted. | ||
if (swapped==False): | ||
break | ||
|
||
# otherwise, reset the swapped flag so that it | ||
# can be used in the next stage | ||
swapped = False | ||
|
||
# move the end point back by one, because | ||
# item at the end is in its rightful spot | ||
end = end-1 | ||
|
||
# from right to left, doing the same | ||
# comparison as in the previous stage | ||
for i in range(end-1, start-1,-1): | ||
if (a[i] > a[i+1]): | ||
a[i], a[i+1] = a[i+1], a[i] | ||
swapped = True | ||
|
||
# increase the starting point, because | ||
# the last stage would have moved the next | ||
# smallest number to its rightful spot. | ||
start = start+1 | ||
|
||
# Driver code to test above | ||
a = [5, 1, 4, 2, 8, 0, 2] | ||
cocktailSort(a) | ||
print("Sorted array is:") | ||
for i in range(len(a)): | ||
print ("%d" %a[i]), |