Question

3. Bubble Sort – Write a program that tests the bubble algorithm. Use an array of...

3. Bubble Sort – Write a program that tests the bubble algorithm. Use an array of 20,000 elements. Calculate and print the time for the sort.

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

Hey here is answer to your question.

In case of any doubt comment below. Please UPVOTE if you Liked the answer.

from random import *
import time

def bubbleSort(arr):
   n = len(arr)
   for i in range(n):
       for j in range(0, n-i-1):
           if arr[j] > arr[j+1] :
               arr[j], arr[j+1] = arr[j+1], arr[j]


seed(1)

arr = []
# generate some integers
for _ in range(20000):
   arr.append(randint(0, 10))

start_time = time.time()

bubbleSort(arr)

print("--- %s seconds are taken to sort 20000 numbers by using bubble sort---" % (time.time() - start_time))


Add a comment
Know the answer?
Add Answer to:
3. Bubble Sort – Write a program that tests the bubble algorithm. Use an array of...
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
  • Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent...

    Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent pair of elements If the pair is not sorted Swap the elements Use the BubbleSorter class to fill in the code and make it run with the BubbleSorterDemo class. BubbleSorter.java public class BubbleSorter {    /** Sort an integer array using the bubble sort algorithm. @param arr array of integers to sort    */    public static void sort(int[] arr)    { // Your...

  • Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays....

    Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...

  • Write in C++ (Bubble Sort) implement the bubble sort algorithm - another simple yet inefficient s...

    Write in C++ (Bubble Sort) implement the bubble sort algorithm - another simple yet inefficient sorting technique. its called bubble sort or sinking sort because smaller values gradually "bubble" their way to the top of the array like air bubbles rising in water, while the larger values sink to the bottom of the array. the technique uses nested loops to make several passes through the array. each pass compares successive pairs of elements. if a pair is in increasing order,...

  • Q1. [10 pts] Write an algorithm for Bubble sort that sorts array of n integers. Indicate...

    Q1. [10 pts] Write an algorithm for Bubble sort that sorts array of n integers. Indicate the expected time complexity of the algorithm using the big-O notation. Use the following format for an algorithm pseudocode Function header (.....) Input: Output: Algorithm steps: 1. 2.

  • MIPS MIPS MIPS PLEASE INCLUDE COMMENTS AND OUTPUT Sort array using Bubble sort algorithm. 1) First...

    MIPS MIPS MIPS PLEASE INCLUDE COMMENTS AND OUTPUT Sort array using Bubble sort algorithm. 1) First ask the user how many elements of his/her array. 2) Then, read the integer array elements as input from the User. 3) Then, print out the array before the sorting 4) Apply Bubble sort algorithm on your array 5) Print out the array after the sorting 6) Print some welcome text to th user 7) Add comments to your code to describe how is...

  • Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and...

    Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and Insertion Sort. The numbers to be sorted will be obtained using a library function which generates pseudo-random numbers. TO Do 1. Fill an array with 140 real numbers between 0.00 and 945.00. Generate the numbers using the subroutine that generates random numbers. Make a spare copy of the array; you will need it later. 2. Call a subroutine to print the contents of the...

  • TO DO: IMPLEMENT SELECTION SORT, BUBBLE SORT, MERGE SORT INSTRUCTIONS: GENERATE AN ARRAY arr AND FILL...

    TO DO: IMPLEMENT SELECTION SORT, BUBBLE SORT, MERGE SORT INSTRUCTIONS: GENERATE AN ARRAY arr AND FILL IT WITH 100 RANDOM INTEGERS, HAVING VALUES 0-99. PRINT THE UNSORTED ARRAY IMPLEMENT EACH SORTING ALGORITHM (DO NOT USE THE BUILT-IN LIBRARIES) FOR EACH ALGORITHM, INCLUDE PSEUDOCODE WITH NUMBERED STEPS. IN YOUR CODE, CLEARLY COMMENT WHICH STEP IS BEING PERFORMED BY THE LINE OR BLOCK OF CODE. USE A TIMER TO CHECK HOW LONG EACH ALGORITHM TAKES TO SORT THE ARRAY. THIS SHOULD BE...

  • write a python program Implement a “bouncing” bubble sort algorithm. In this version if bubble sort,...

    write a python program Implement a “bouncing” bubble sort algorithm. In this version if bubble sort, instead of making passes through a list that starts at the beginning andirons through to the end, you should reverse the direction each pass.That is , if the first pass starts at the beginning of the list and runs through to the end, the second pass out run from the end of the list back to the beginning and then the third pass would...

  • Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers

    Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

  • Using C++, sort an array of 10,000 elements using the quick sort algorithm as follows: a....

    Using C++, sort an array of 10,000 elements using the quick sort algorithm as follows: a. Sort the array using pivot as the middle element of the array. b. Sort the array using pivot as the median of the first, last, and middle elements of the array. c. Sort the array using pivot as the middle element of the array. However, when the size of any sublist reduces to less than 20, sort thesublis t using an insertion sort. d....

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