Skip to content

sorting #939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Sorting Algorithims/HeapSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# heapify
def heapify(arr, n, i):
largest = i # largest value
l = 2 * i + 1 # left
r = 2 * i + 2 # right
# if left child exists
if l < n and arr[i] < arr[l]:
largest = l
# if right child exits
if r < n and arr[largest] < arr[r]:
largest = r
# root
if largest != i:
arr[i],arr[largest] = arr[largest],arr[i] # swap
# root.
heapify(arr, n, largest)
# sort
def heapSort(arr):
n = len(arr)
# maxheap
for i in range(n, -1, -1):
heapify(arr, n, i)
# element extraction
for i in range(n-1, 0, -1):
arr[i], arr[0] = arr[0], arr[i] # swap
heapify(arr, i, 0)
# main
arr = [2,5,3,8,6,5,4,7]
heapSort(arr)
n = len(arr)
print ("Sorted array is")
for i in range(n):
print (arr[i],end=" ")
26 changes: 26 additions & 0 deletions Sorting Algorithims/InsertionSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Python program for implementation of Insertion Sort

# Function to do insertion sort
def insertionSort(arr):

# Traverse through 1 to len(arr)
for i in range(1, len(arr)):

key = arr[i]

# Move elements of arr[0..i-1], that are
# greater than key, to one position ahead
# of their current position
j = i-1
while j >=0 and key < arr[j] :
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key


# Driver code to test above
arr = [12, 11, 13, 5, 6]
insertionSort(arr)
print ("Sorted array is:")
for i in range(len(arr)):
print ("%d" %arr[i])
56 changes: 56 additions & 0 deletions Sorting Algorithims/QuickSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Python program for implementation of Quicksort Sort

# This function takes last element as pivot, places
# the pivot element at its correct position in sorted
# array, and places all smaller (smaller than pivot)
# to left of pivot and all greater elements to right
# of pivot


def partition(arr, low, high):
i = (low-1) # index of smaller element
pivot = arr[high] # pivot

for j in range(low, high):

# If current element is smaller than or
# equal to pivot
if arr[j] <= pivot:

# increment index of smaller element
i = i+1
arr[i], arr[j] = arr[j], arr[i]

arr[i+1], arr[high] = arr[high], arr[i+1]
return (i+1)

# The main function that implements QuickSort
# arr[] --> Array to be sorted,
# low --> Starting index,
# high --> Ending index

# Function to do Quick sort


def quickSort(arr, low, high):
if len(arr) == 1:
return arr
if low < high:

# pi is partitioning index, arr[p] is now
# at right place
pi = partition(arr, low, high)

# Separately sort elements before
# partition and after partition
quickSort(arr, low, pi-1)
quickSort(arr, pi+1, high)


# Driver code to test above
arr = [10, 7, 8, 9, 1, 5]
n = len(arr)
quickSort(arr, 0, n-1)
print("Sorted array is:")
for i in range(n):
print("%d" % arr[i])
26 changes: 26 additions & 0 deletions Sorting Algorithims/bubblesort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Python program for implementation of Bubble Sort

def bubbleSort(arr):
n = len(arr)

# 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] :
arr[j], arr[j+1] = arr[j+1], arr[j]

# 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]),
25 changes: 25 additions & 0 deletions Sorting AlgorithimsSort.py/CountingSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
def countSort(arr):
# The output character array that will have sorted arr
output = [0 for i in range(256)]
# Create a count array initialized with 0
count = [0 for i in range(256)]
# as strings are immutable
ans = ["" for _ in arr]
# count
for i in arr:
count[ord(i)] += 1
# position of character in the output array
for i in range(256):
count[i] += count[i-1]
# output character array
for i in range(len(arr)):
output[count[ord(arr[i])]-1] = arr[i]
count[ord(arr[i])] -= 1
# array of sorted charcters
for i in range(len(arr)):
ans[i] = output[i]
return ans
# main
arr = "Tutorialspoint"
ans = countSort(arr)
print ("Sorted character array is "+str("".join(ans))
38 changes: 38 additions & 0 deletions Sorting AlgorithimsSort.py/CycleSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
def cycleSort(array):
writes = 0
# cycles to be rotated
for cycleStart in range(0, len(array) - 1):
item = array[cycleStart]
#position to place the item
pos = cycleStart
for i in range(cycleStart + 1, len(array)):
if array[i] < item:
pos += 1
# if item exits, it is not a cycle
if pos == cycleStart:
continue
# Otherwise, place the item
while item == array[pos]:
pos += 1
array[pos], item = item, array[pos]
writes += 1
# rotation continued
while pos != cycleStart:
# Find a position to place the item
pos = cycleStart
for i in range(cycleStart + 1, len(array)):
if array[i] < item:
pos += 1
# place the item
while item == array[pos]:
pos += 1
array[pos], item = item, array[pos]
writes += 1
return writes
# main
arr = [1,5,3,4,8,6,3,4,5]
n = len(arr)
cycleSort(arr)
print("Sorted array is : ")
for i in range(0, n) :
print(arr[i], end = " ")