Question

Python

If it is possible, please do not use hand writing form, thank you.

Question 2: What do you know about the sorting algorithms? Explain Merge sort with example and write a Program or merge sort

0 0
Add a comment Improve this question Transcribed image text
Answer #1

A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most frequently used orders are numerical order.

Some commonly used sorting algos:

Selection Sort: time complexity  O(n2)

Bubble Sort : time complexity О(n2)

Insertion Sort: time complexity O(n*2)

Merge Sort : time complexity O(n log n)

QuickSort : time complexity O(n log n)

HeapSort: time complexity O(Logn)

MERGE SORT

Divide the unsorted list into n sublists, each comprising 1 element (a list of 1 element is supposed sorted).

p 0 q 3 1 2 r 6 7 4 5 14 7 3 12 9 11 6 2 divide p q r 0 1 2 3 p 9 r 4 5 6 7 14 7 3 12 9 11 6 2 divide r p, 0 p,q r p, 4 r 5 2

Repeatedly merge sublists to produce newly sorted sublists until there is only 1 sublist remaining. This will be the sorted list.

The first element of both lists is compared. If sorting in ascending order, the smaller element among two becomes a new element of the sorted list. This procedure is repeated until both the smaller sublists are empty and the newly combined sublist covers all the elements of both the sublists.

r p, r 0 1 p, 2 p, 4 r 5 p, r 6 7 3 7 14 3 12 9 11 2 6 merge q p 0 1 2 r 2 3 р q r 4 5 6 7 3 7 12 14 2 6 9 11 merge r р 0 1 2source code:-

def mergeSort(myList):
if len(myList) > 1:
mid = len(myList) // 2
left = myList[:mid]
right = myList[mid:]

# Recursive call on each half
mergeSort(left)
mergeSort(right)

# Two iterators for traversing the two halves
i = 0
j = 0
  
# Iterator for the main list
k = 0
  
while i < len(left) and j < len(right):
if left[i] < right[j]:
# The value from the left half has been used
myList[k] = left[i]
# Move the iterator forward
i += 1
else:
myList[k] = right[j]
j += 1
# Move to the next slot
k += 1

# For all the remaining values
while i < len(left):
myList[k] = left[i]
i += 1
k += 1

while j < len(right):
myList[k]=right[j]
j += 1
k += 1

myList = [14,7,3,12,9,11,6,2]
mergeSort(myList)
print(myList)

Add a comment
Know the answer?
Add Answer to:
Python If it is possible, please do not use hand writing form, thank you. Question 2:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please write in python. thank you In this question you will be writing the code to...

    please write in python. thank you In this question you will be writing the code to carry out a query on a database table in Python. The CREATE command shown below is given just for your reference, so that you know the structure of the table and its columns. The database is an SQLite database in the file named taxa.sqlite. create table species ( id int primary key, genus varchar ( 5 0 ), species varchar(50), common_name char(100) ) Write...

  • PLEASE DO BOTH #5 AND #6. The purpose of the project is to perform a timing...

    PLEASE DO BOTH #5 AND #6. The purpose of the project is to perform a timing experiment. You are required to complete the following activities: Write a computer program that prompts the user for a number, creates an array for that number of random integers, and then usees the bubble sort to order the array. The program should print out the array prior to the call to the sorting algorithm and afterwards. You can write the program in either Java,...

  • The purpose of the project is to perform a timing experiment. You are required to complete...

    The purpose of the project is to perform a timing experiment. You are required to complete the following activities: Write a computer program that prompts the user for a number, creates an array for that number of random integers, and then usees the bubble sort to order the array. The program should print out the array prior to the call to the sorting algorithm and afterwards. You can write the program in either Java, C++, C#, or whatever language you...

  • Here is the code given for this problem: **And please do this in Python** def mergesort(mlist): if len(mlist)<2: ret...

    Here is the code given for this problem: **And please do this in Python** def mergesort(mlist): if len(mlist)<2: return mlist else: mid=len(mlist)//2 return merge(mergesort(mlist[:mid]),mergesort(mlist[mid:])) Problem 1 (30 points) stable merge sort Consider the following unsorted list of integers containing duplicates where the relative position of each duplicated integer in the unsorted list is noted as a subscript. For example, 1, is at a smaller index than 12. The subscript is ignored when comparing two values since it would not actually...

  • ?PLEASE READ CAREFULLY QUESTION #1 I’m doing a project which requires you to implement 4 sorting...

    ?PLEASE READ CAREFULLY QUESTION #1 I’m doing a project which requires you to implement 4 sorting algorithms. Bubble sort pair-wise, Bubble sort list-wise a.k.a selection sort, merge sort, and quick sort. These 4 sorting methods takes in an array of strings and sorts them alphabetically from a-z. I have all 4 sorting algorithms working fine, but I still need to fill out the table. There’s only one section I need help filling out. I basically need help filling out the...

  • PLEASE DO NOT USE HAND WRITING     PLEASE DO NOT USE HAND WRITING     PLEASE DO NOT USE HAND WRITING PLEASE DO NOT USE HAND WRITING 6. For the problem given in Question 5, suppose the current automate...

    PLEASE DO NOT USE HAND WRITING     PLEASE DO NOT USE HAND WRITING     PLEASE DO NOT USE HAND WRITING PLEASE DO NOT USE HAND WRITING 6. For the problem given in Question 5, suppose the current automated machine can be replaced by a more efficient automated machine which requires $30 per hour of leasing cost whereas the current automated machine is leased at $20 per hour. The more efficient automated machine can prepare a license in 5 minutes. If a driver’s...

  • Please help with this python assignment. Thank you. question 1 Write a Python program to read...

    Please help with this python assignment. Thank you. question 1 Write a Python program to read a file line by line store it into a variable. question 2 Write a Python program to read a file line by line store it into an array. question 3 Write a python program to find the longest words. question 4 Write a Python program to count the number of lines in a text file. question 5 Write a Python program to count the...

  • does anyone know how to do this? C++ Your task for this assignment is to use...

    does anyone know how to do this? C++ Your task for this assignment is to use C++ language to implement an insertion sort algorithm. 1. Implement an insertion sort with an array to sort 10 arbitrary numbers using the C++ programming language. The program initializes an array with 10 random 3-digit positive integers. The program then sorts the numbers in the array using the insertion sort algorithm. 2. The program displays the original values in the array before the sort...

  • Programming Language : Python Question a) Are there any programming framework for this language? If yes,...

    Programming Language : Python Question a) Are there any programming framework for this language? If yes, where and how do you get them (URL) ? b) What is (are) the program development environments (s) available for this language? and what is provided in each program development environment?

  • please answer with typing not with hand writing and if you can do MLA style Thank...

    please answer with typing not with hand writing and if you can do MLA style Thank you its okay without MLA Hi class, We've recently talked about the different states of energy that our body is constantly transferring to and from. Our body naturally produces energy in the form of ATP from the food that we ingest. It takes in the high energy content from the macromolecules and converts that into small, less complex molecules that carry out other functions....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT