You are on page 1of 1

import sys

def partition(ls_to_sort, low, high):


pivot = ls_to_sort[high]
i = low-1

for j in range(low, high):


if ls_to_sort[j] < pivot:
i = i+1
ls_to_sort[i], ls_to_sort[j] = ls_to_sort[j], ls_to_sort[i]

ls_to_sort[i+1], ls_to_sort[high] = ls_to_sort[high], ls_to_sort[i+1]


return i+1

def quick_sort(ls_to_sort, low, high):


if low < high:
pivot = partition(ls_to_sort, low, high)
quick_sort(ls_to_sort, low, pivot-1)
quick_sort(ls_to_sort, pivot+1, high)
return ls_to_sort

def main():
# for matrices
# file = open("/home/max/q_sort.txt", "r")
# file_handle = [[int(x) for x in line.split()] for line in file]
# quick_sort(file_handle, 0, len(file_handle)-1)
# print(file_handle)

# for matrices using 'with'


# with open('/home/max/q_sort_2.txt') as f:
# num_array = [[int(x) for x in line.split()] for line in f]

# two ways to read a line


# 1:
# with open('/home/max/q_sort.txt') as f:
# num_array = [int(x) for x in f.readline().split()]
# 2:
# num_array = []
# with open('/home/max/q_sort_2.txt') as f:
# for x in f.readline().split():
# num_array.append(int(x))

# for matrices using numpy


# import numpy as np
# num_array = np.loadtxt('/home/max/q_sort_3.txt')[:, 0]

with open(sys.argv[1]) as f:
num_array = [int(x) for x in f.readline().split()]

quick_sort(num_array, 0, len(num_array)-1)
print(num_array)

if __name__ == '__main__':
main()

You might also like