Bubble sort and quick sort in ascending order

Assignment Help Data Structure & Algorithms
Reference no: EM13189279

Question 1:

(a) Bubble sort in ascending order.

1. Pseudo code:

FUNCTION BubbleSortIMPORT array EXPORT array

FOR pass ¬ 0 TO (array.length-1)-1 DO¬Need N-1 passes to guarantee sorted

FOR ii ¬ 0 TO (array.length-1 - pass)-1 DO¬NOTE: 0-based array indexing

IF (array[ii] > array[ii+1]) THEN¬Avoid >= to keep the sort stable

temp ¬ array[ii]¬Swap out-of-order elements ii and ii+1

array[ii] ¬ array[ii+1]

array[ii+1] ¬ temp

ENDIF

ENDFOR

ENDWHILE

(b) Quick sort in ascending order, with partition choosing pivot in the middle of the sub-array.

2. Pseudo code:

FUNCTION QuickSort IMPORT array, leftIdx, rightIdx EXPORT array

IF (rightIdx > leftIdx) THEN¬Check that the array is > one element in size

pivotIdx¬ (leftIdx+rightIdx) / 2¬Pivot selection strategy: middle element

newPivotIdx¬ doPartitioning(array, leftIdx, rightIdx, pivotIdx)

QuickSort(array, leftIdx, newPivotIdx-1)¬Recurse: Sort left partition

QuickSort(array, newPivotIdx+1, rightIdx)¬Recurse: Sort right partition

//ELSE

// Base case: array is 1 element or smaller in size - already sorted

ENDIF

ENDFUNCTION

FUNCTION doPartitioning IMPORT array, leftIdx, rightIdx, pivotIdx EXPORT newPivotIdx

pivotVal¬ array[pivotIdx]

array[pivotIdx] ¬ array[rightIdx]¬Swap the pivotVal with the right-most element

array[rightIdx] ¬ pivotVal

// Find all values that are smaller than the pivot

// and transfer them to the left-hand-side of the array

currIdx¬ leftIdx

FOR (ii ¬ leftIdx TO rightIdx-1)

IF (array[ii] <pivotVal) THEN¬Find the next value that should go on the left

temp¬ array[ii]¬Put this value to the left-hand-side

array[ii] ¬ array[currIdx]

array[currIdx] ¬ temp

currIdx¬ currIdx + 1

ENDIF

ENDFOR

newPivotIdx¬ currIdx

array[rightIdx] ¬ array[newPivotIdx]¬Put the pivot into its rightful place (the value at

array[newPivotIdx] ¬ pivotVal[newPivotIdx] is >= pivotVal, so it can be put to the end)

ENDFUNCTION

(c) Shell sort in ascending order, with initial increment = n/2, then increment /=2.

3. Pseudo code:

 

FUNCTIONShellSortIMPORTsize

   for (inc¬size/2 inc>0inc /= 2)

 

      for (i¬inci< sizei++)

 

         j¬i - inc

         while (j >= 0)

 

            if (a[j] > a[j+inc])

 

               swap a[j] and a[j+inc]

               j -¬inc

            else

               j¬-1

ENDFUNCTION

(d)   Heap sort in ascending order.

4. Pseudo code:

 

FUNCTIONHeapSortIMPORTsize

   for (i¬ size/2 i>= 0i--)

      ReHeap(size, i)

   for (i¬ size-1 i> 0i--)

 

      swap a[i] and a[0]

      ReHeap(i, 0)

ENDFUNCTION

 


FUNCTIONReHeapIMPORTlen, parent

   temp¬a[parent]

   child¬2*parent + 1

   while (child <len)

 

      if (child<len-1 and a[child]<a[child+1])

         child++

      if (temp >= a[child])

      break

      a[parent] = a[child]

      parent¬child

      child ¬2*parent + 1

 

  a[parent] ¬temp

ENDFUNCTIONTime Analysis of Mergesort

Assumption: N is a power of two.

For N = 1: time is a constant (denoted by 1)

Otherwise: time to mergesort N elements = time to mergesort N/2 elements plus time to merge two arrays each N/2 elements.

Time to merge two arrays each N/2 elements is linear, i.e. N

Thus we have:

(1) T(1) = 1

(2) T(N) = 2T(N/2) + N

Next we will solve this recurrence relation. First we divide (2) by N:

(3) T(N) / N = T(N/2) / (N/2) + 1

N is a power of two, so we can write

(4) T(N/2) / (N/2) = T(N/4) / (N/4) +1

(5) T(N/4) / (N/4) = T(N/8) / (N/8) +1

(6) T(N/8) / (N/8) = T(N/16) / (N/16) +1

(7) ......
(8) T(2) / 2 = T(1) / 1 + 1

Now we add equations (3) through (8) : the sum of their left-hand sides
will be equal to the sum of their right-hand sides:

T(N) / N + T(N/2) / (N/2) + T(N/4) / (N/4) + T(2)/2 =

T(N/2) / (N/2) + T(N/4) / (N/4) + T(2) / 2 + T(1) / 1 + LogN

(LogN is the sum of 1s in the right-hand sides)

After crossing the equal term, we get

(9) T(N)/N = T(1)/1 + LogN

T(1) is 1, hence we obtain

(10) T(N) = N + NlogN = O(NlogN)

Hence the complexity of the MergeSort algorithm is O(NlogN).

Reference no: EM13189279

Questions Cloud

Characteristic of a realigning election : All of the following are characteristic of a realigning election except
What is the maximum number of whole gallons of paint : If one gallon of paint has a volume of approximately 4000 cm3, what is the maximum number of whole gallons of paint that can be poured into the bucket?
What percent of gdp does the us spend on health care : What percent of GDP does the US spend on Health care The UK Canada Do you think the United States spends too much on medical care Explain your reasoning using the PPF model List and explain the 3 reasons for this increase.
Is rare condition in which brain fails to develop at levels : Is a rare condition in which the brain fails to develop at levels above the
Bubble sort and quick sort in ascending order : Quick sort in ascending order, with partition choosing pivot in the middle of the sub-array.
Find the mean of the distribution shown : Find the mean of the distribution shown.
How to set up and solve equations and inequalities : What is the importance of understanding how to set up and solve equations and inequalities.
How the program to impact the firms bottom line : Ford executives announced that the company would extend its most dramatic consumer incentive program in the company's long history-- the Ford Drive America Program. The program provides consumers with either cash back or zero percent financing for..
What about the second equation : How are they different? Find a problem in the text that is similar to examples 2, 3, and 4. Post the problem for your classmates to solve.

Reviews

Write a Review

Data Structure & Algorithms Questions & Answers

  Shell scripting based questions

Determine will the following only print the text "I FOUND A MATCH" to standard output when the grep is successful? if grep "mrichard" /etc/passwd; then echo "I FOUND A MATCH"; fi

  Conversion with unnormalized relation

Nazca Cinemas is a tiny movie theater that would like you to create a movie scheduling database system for them. The theater has 4-screens. Each screens has its own unique ID

  The binary search algorithm

- The "origin" of the Cartsian plane in math is the point where x and y are both zero. Declare a variable of type POINT named origin and set its data dields consistent with the mathematical notion of "origin".

  Implement a queue as a circular array

Implement a queue as a circular array as follows: Use two index variables head and tail that contain the index of the next element to be removed and the next element to be added.

  Online vs. face-to-face classes

Communication A significant distinction between online and face-to-face classes lies in the area of communication.

  Build b tree for the part table

Build B+ tree for the PART table with n = 6 pointers; illustrate how B+ tree expand (show several intermediate trees) and what final tree will look like.

  Algorithm to minimize average difference between height

The problem is to assign each skier a ski to minimize the average difference between height of a skier and his/her ski. Give pseudocode and write its asymptotic running time.

  Different levels of a dbms

Recognize the level within a database system user and designer of the DBMS software at which each of the following concerns or activities happen,

  Insertion sort and merged using standard merging mechanism

Using "insertion sort" and then merged using standard merging mechanism, where k is value to be determined. How must be we select k in practice?

  Explain good algorithms to solve character pathfinding

You are working on the new computer game. One of implementation problems you are trying to solve is character pathfinding. What algorithms would be good to use and explain why?

  Find capacity of a particular airplane type

Consider the entities and their attributes. You should 1st determine what entities want to track. Next determine what attributes are required for each entity, and what relations exist between these entities.

  Discuss and define complex data binding

Discuss and define complex data binding and what benefits can this capability lend to a multiple table database application?

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd