Question

20. Change the bubble sort algorithm (Program 12-5) as follows: Use two directional bubbling in each pass. In the first bubbl

in C please
12-5 Bubble Sort (continued) // Statements // Each iteration is one sort pass for (int current = 0, sorted = 0; current <= la
0 0
Add a comment Improve this question Transcribed image text
Answer #1

void shakerSort(int a[], int n)

{

    bool swapped = true;

    int start = 0;

    int end = n - 1;

  

    while (swapped) {

        swapped = false;

        for (int i = start; i < end; ++i) {

            if (a[i] > a[i + 1]) {

                swap(a[i], a[i + 1]);

                swapped = true;

            }

        }

        if (!swapped)

            break;

        swapped = false;

        --end;

        for (int i = end - 1; i >= start; --i) {

            if (a[i] > a[i + 1]) {

                swap(a[i], a[i + 1]);

                swapped = true;

            }

        }

        ++start;

    }

}

Add a comment
Know the answer?
Add Answer to:
in C please 20. Change the bubble sort algorithm (Program 12-5) as follows: Use two directional...
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
  • Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template...

    Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template of the solution, if you need Write a program that asks users to input 10 integers into an array, write a function that takes the array as its argument, use bubble sort to sort the array and output the sorted array in increasing order. #include <iostream> using namespace std; C:IWINDOWSSystems2cmd.exe lease input 10 integers: void bubblesort(int a[10]) int help; int b[10]; for (int i...

  • Write a program that compares the execution speed of two different sorting algorithms: bubble sort and...

    Write a program that compares the execution speed of two different sorting algorithms: bubble sort and selection sort. It should do this via functions you must write with the following prototypes: void setRandomValues(int[], int[], int length); This function sets two integer arrays with identical random values. The first two arguments are two integer arrays of the same length, and the third argument is the length of those arrays. The function then sets each element in the first and second argument...

  • Consider the following python code that will sort the list of numbers using the Bubble Sort...

    Consider the following python code that will sort the list of numbers using the Bubble Sort algorithm def bubbleSort(alist): print(alist) for j in range (len(alist) - 1, 8, -1): for i in range(): if alist[i] > alist[i + 1]: temp = alist[i] alist[i] = alist[i+1] alist[i+1] = temp print(alist) alist = [52, 25, 94, 17, 25, 52] bubbleSort (alist) print(alist) Sort the following series of values into ascending order using the Bubble Sort algorithm. Write out the complete row of...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • Another simple sort is the odd-even sort. The idea is to repeatedly make two passes through the a...

    Another simple sort is the odd-even sort. The idea is to repeatedly make two passes through the array. On the first pass you look at all the pairs of items, a[j] and a[j+1], where j is odd (j = 1, 3, 5, …). If their key values are out of order, you swap them. On the second pass you do the same for all the even values (j = 2, 4, 6, …). You do these two passes repeatedly until...

  • Programming language: Java Home Work No.2 due 09.11.2019 either ascending or descending SORTING: An array is...

    Programming language: Java Home Work No.2 due 09.11.2019 either ascending or descending SORTING: An array is said to be ordered if its values order. In an ascending ordered array, the value of each element is less than or equal to the value of the next element. That is, [each element] <= [next element]. A sort is an algorithm for ordering an array. Of the many different techniques for sorting an array we discuss the bubble sort It requires the swapping...

  • 3. Modify the insertion sort algorithm discussed in class so that it can sort strings. That...

    3. Modify the insertion sort algorithm discussed in class so that it can sort strings. That is, its input will be an array, the type of which is string. The insertion sort algorithm will sort the elements in this array. Finally, its output will be a sorted array. As the solution of this problem, you need to submit the following answer(s): (1). The implementation of your algorithm in C# (20points). Algorithm: At each array-position, it checks the value there against...

  • 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...

  • [5 marks] Using selection sort algorithm to sort the array int array[7]-5, 6, 2, 7, 9,...

    [5 marks] Using selection sort algorithm to sort the array int array[7]-5, 6, 2, 7, 9, 4, 3). Assuming we call the following function using statement selection sort (int array, 7); Please fill the table to show the changes of the array int_array after each iteration. The first column is filled. void selection_sort (int list[], int list_size) for (int i = 0; i < list size - 1; 1++) int current min = list[1]; int current_min_index-i for (int j -...

  • ********** PLEASE PROVIDE IN JAVA & NEW SET OF ALGORITHM For this lab use the program...

    ********** PLEASE PROVIDE IN JAVA & NEW SET OF ALGORITHM For this lab use the program at the bottom to sort an array using selection sort. Write a selection sort to report out the sorted low values: lowest to highest. Write another to report out the sorted high values – highest to lowest. Last but not least, the program should output all the values in the array AND then output the 2 requested sorted outputs. -----> MY OLD PROGRAM BELOW...

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