Write 3 programs in Python Implement the Bubble Sort Impleme

Write 3 programs in Python:

Implement the Bubble Sort

Implement the Insertion Sort

Implement the Selection Sort

Generate a file that has 5000 random numbers in it. Use that file to test your sorts.

Solution

Here is the code to generate 5000 random numbers:

#!/usr/bin/python
def dowrite():
   import random
   num = 5000
   fileOut = open(\"RandomIntegers.txt\", \"w\")
   for i in range(num):
       fileOut.write(\"%d\ \" % random.randint(1, 100))
   fileOut.close()

def main():
   dowrite()
  
main()

And the code for BubbleSort.py is:

#!/usr/bin/python
def bubbleSort(list):
   for i in range(len(list) - 1):
       for j in range(len(list) - i - 1):
           if list[j] > list[j+1]:
               temp = list[j]
               list[j] = list[j+1]
               list[j+1] = temp
def doread():
   fileIn = open(\"RandomIntegers.txt\", \"r\")
   list = []
   for number in fileIn:
       list.append(number)
   fileIn.close
   return list
  
list = doread()  
print list[0]
bubbleSort(list)
print list[0]

And the code for InsertionSort.py is:

Write 3 programs in Python: Implement the Bubble Sort Implement the Insertion Sort Implement the Selection Sort Generate a file that has 5000 random numbers in

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site