Question

In: Computer Science

Sorting (selection, bubble, insertion) Suppose an array contains the following integers: -1, 20, 10, -5, 0,...

Sorting (selection, bubble, insertion)

Suppose an array contains the following integers: -1, 20, 10, -5, 0, -7, 100, -7, 30, -10. Sort the array using the following algorithms: selection sort, bubble sort, and insertion sort. For each algorithm, write the array content after each pass (i.e., from pass 1 to pass 9). Each pass is defined as one iteration of the outer loop. There are total of n − 1 passes, where n is the array size.


The answer should not be Java or code

Solutions

Expert Solution

ANSWER:

def selectionSort(l):
    for i in range(len(l)-1):
        print("Pass",i+1,"for Selection Sort:",l)
        index=i
        for j in range(i+1,len(l)): 
            if l[index] > l[j]: 
                index = j          
        l[i],l[index] = l[index],l[i]
    print("Final Sorted Array:",l)

def insertionSort(l):
    for i in range(1, len(l)):
        element = l[i]
        j = i-1
        print("Pass",i,"for Insertion Sort:",l)
        while(j >= 0 and element < l[j]): 
                l[j + 1] = l[j]
                j -= 1
        l[j + 1] = element
    print("Final Sorted Array:",l)

def bubbleSort(l):
    n= len(l)
    for i in range(n-1):
        print("Pass",i+1,"for Bubble Sort:",l)
        for j in range(0, n-i-1): 
            if(l[j] > l[j+1]): 
                l[j],l[j+1] = l[j+1],l[j]
    print("Final Sorted Array:",l)
    



l= [-1, 20, 10, -5, 0, -7, 100, -7, 30, -10]
selectionSort(list(l))
print("-------------------------------------")
insertionSort(list(l))
print("-------------------------------------")
bubbleSort(list(l))


NOTE: The above code is in Python3. Please refer to the attached screenshots for code indentation and sample I/O.

SAMPLE I/O:


Related Solutions

In Python, there are different sorting algorithms. Selection Sort, Bubble Sort and Insertion Sort. • Write...
In Python, there are different sorting algorithms. Selection Sort, Bubble Sort and Insertion Sort. • Write a Pseudo code first for each of these sort methods.   • After writing the pseudo code write python code from the pseudo code. • Upload the pseudo code and Python code for each of the three algorithm mentioned.
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the...
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the range of 0 to 100. There may be repeats. The numbers must not be ordered/sorted. The task is to find and print the two smallest numbers. You must accomplish this task without sorting the file and without using arrays for any purpose. It is possible that the smallest numbers are repeated – you should print the number of occurrences of the two smallest numbers....
c++ Run the following sorting algorithms: 1. Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort...
c++ Run the following sorting algorithms: 1. Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort Under the following scenarios for input data: 1. Uniform random 2. Almost sorted (90% sorted – 1 in 10 is out of place) 3. Reverse sorted On data of sizes 5,000, 10,000, … in increments of 5,000 up to …, 50,000 -Attach a screenshot of a program compilation below -Attach a screenshot of a successful program run below -Attach a graph (either line graph...
Sorting – Insertion Sort Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the...
Sorting – Insertion Sort Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the list after each outer loop. Do his manually, i.e. step through the algorithm yourself without a computer. This question is related to data structure and algorithm in javascript (.js). Please give your answer keeping this in your mind.
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick...
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick sort? Give a simple scheme that makes any sorting algorithm stable. How much additional time and space does your scheme entail?
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the...
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the process step-by-step, and find the time complexity in Big-O notation for each method. For sorting, use ascending order. 49, 7, 60, 44, 18, 105
Bubble Sort is performed on an array with values [6, 1, 2, 0, 5, 4]. What...
Bubble Sort is performed on an array with values [6, 1, 2, 0, 5, 4]. What are the array values after two passes of the Bubble Sort? The largest value in the array is moved to the correct location after each pass.
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers,...
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers, lowIndex, highIndex) {    midpoint = lowIndex + (highIndex - lowIndex) / 2    pivot = numbers[midpoint]    done = false    while (!done) {       while (numbers[lowIndex] < pivot)          lowIndex++       while (pivot < numbers[highIndex])          highIndex--       if (lowIndex >= highIndex) {          done = true       }       else {          temp = numbers[lowIndex]          numbers[lowIndex] = numbers[highIndex]          numbers[highIndex] = temp                 lowIndex++          highIndex--       }    }    return highIndex } Quicksort(numbers, lowIndex, highIndex) {    if (lowIndex...
Run Solver.java, where 20 array instances of 1M random integers are sorted (using Insertion- sort and...
Run Solver.java, where 20 array instances of 1M random integers are sorted (using Insertion- sort and Heap-sort, respectively). Solver.java is given, however the two sorting algorithms of course are implemented by yourself. Report the time elapsed running Solver.java, using Insertion-sort, and Heap-sort, respectively. Based on your results, comment comparatively on time-efficiency of the two sorting algorithms ////// public class Solver {    public static void main(String[] args) { final int SIZE = 1000000; // 1 million final int Instances=20; int[][]...
Suppose we are sorting an array of eight integers using quicksort, and we have just finished...
Suppose we are sorting an array of eight integers using quicksort, and we have just finished the first partitioning with the array looking like this: 2 5 1 7 9 12 11 10. What are the possible values of pivot? Algorithm coures
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT