Bubble Sort, Sorting Complexity Problems - Project 3 | CS 1112, Study Guides, Projects, Research of Computer Science

Material Type: Project; Class: Introduction to Computing Using MATLAB; Subject: Computer Science; University: Cornell University; Term: Fall 2008;

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 08/31/2009

koofers-user-czu
koofers-user-czu 🇺🇸

5

(1)

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS1112Fall2008Project3dueThursday10/9at6pm
Youmustworkeitheronyourownorwithonepartner.Youmaydiscussbackgroundissuesandgeneralsolution
strategieswithothers,buttheprojectyousubmitmustbetheworkofjustyou(andyourpartner).Ifyouwork
withapartner,youandyourpartnermustfirstregisterasagroupinCMSandthensubmityourworkasagroup.
Objectives
Togainfamiliarityandexperienceinthefollowing:
Userdefinedfunctions
Problemdecomposition
Vectors(1darray)ofnumbers
Therandfunction
BeforeYouBegin
Readtheentireprojectbeforeyoubegin;mostoftheproblemsinthisprojectareinterrelated.
Readtheinstructionsinthemfilesthoroughlybeforewritinganycode;additionalfunctional
requirementsareplacedinthecommentsinthosefiles
Background:Sorting
Youwillimplementvarioussortingalgorithmsforthisassignment.Sortingagroupofelementsisextremely
commoninprogrammingformanyreasons:
Humanusersusuallyprefersortedoutput
Sortingstoreddata,suchasadatabase,increasestheefficiencyofinformationretrieval
Sortingisoftenthefirststeptosolvingabiggerproblem
Forthisassignment,youwillsortarraysofnumbersinascendingorder.Youwillimplementthreefamoussorting
algorithms.
Problem1:BubbleSort
Bubblesortisoneofthesimplestsortingalgorithms.Itworksbyrepeatedlyiteratingthruthearraytobesorted,
comparingtwoadjacentnumbersinthearray,andswappingthemiftheyareinthewrongorder.Theiterations
thruthelistarerepeateduntilnoswapsareneeded,whichmeansthatthearrayissorted.
Inordertoimplementbubblesort,youcandecomposetheproblemintosmallersubproblems:
Testingiftwonumbersareintheproperorder
Swappingtwonumbersinanarray
a) SolvetheabovesubproblemsbycompletingthefunctionsfoundatProperOrder.m,andSwapElements.m.
Thesemfilescanbefoundattheclasswebsite,andtheycontaincommentsthatdefineeachofthesesub
problemsinmoredetail.
pf3

Partial preview of the text

Download Bubble Sort, Sorting Complexity Problems - Project 3 | CS 1112 and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

CS1112 Fall 2008 Project 3 due Thursday 10/9 at 6pm

You must work either on your own or with one partner. You may discuss background issues and general solution strategies with others, but the project you submit must be the work of just you (and your partner). If you work with a partner, you and your partner must first register as a group in CMS and then submit your work as a group.

Objectives

To gain familiarity and experience in the following:

  • User‐defined functions
  • Problem decomposition
  • Vectors (1‐d array) of numbers
  • The rand function

Before You Begin

  • Read the entire project before you begin; most of the problems in this project are interrelated.
  • Read the instructions in the m‐files thoroughly before writing any code; additional functional requirements are placed in the comments in those files

Background: Sorting

You will implement various sorting algorithms for this assignment. Sorting a group of elements is extremely common in programming for many reasons:

  • Human users usually prefer sorted output
  • Sorting stored data, such as a database, increases the efficiency of information retrieval
  • Sorting is often the first step to solving a bigger problem For this assignment, you will sort arrays of numbers in ascending order. You will implement three famous sorting algorithms.

Problem 1: Bubble Sort

Bubble sort is one of the simplest sorting algorithms. It works by repeatedly iterating thru the array to be sorted, comparing two adjacent numbers in the array, and swapping them if they are in the wrong order. The iterations thru the list are repeated until no swaps are needed, which means that the array is sorted. In order to implement bubble sort, you can decompose the problem into smaller sub problems:

  • Testing if two numbers are in the proper order
  • Swapping two numbers in an array a) Solve the above sub‐problems by completing the functions found at ProperOrder.m , and SwapElements.m. These m‐files can be found at the class website, and they contain comments that define each of these sub problems in more detail.

b) Implement the bubble sort function by completing BubbleSort.m. You must use the ProperOrder and SwapElements functions wherever possible in your implementation. Submit ProperOrder.m, SwapElements.m and BubbleSort.m to CMS.

Problem 2: PMerge Sort (Partial Merge Sort)

Merge sort is another famous sorting algorithm. We will not be implementing merge sort in its full beauty. Instead, we will implement PMerge sort, a sorting algorithm that takes advantage of one of the core ideas used in real merge sort.

  • First, you will sort the first half of the array using the BubbleSort function from part 1
  • Then, you will sort the second half of the array using the BubbleSort function from part 1
  • Lastly, you will merge these two sorted sub‐arrays to obtain the final sorted array. You have already developed the code to solve the first two parts listed above. Consider the last part: You are given two sorted arrays, and you need to create a new sorted array that has all the elements that the two smaller arrays had. a) Implement this merge operation by completing the function found at MergeArrays.m. You cannot perform any additional sorting in this function. b) Implement the PMerge sort by completing PMergeSort.m. You must use the BubbleSort and MergeArrays functions wherever possible in your implementation. Submit MergeArrays.m , and PMergeSort.m to CMS.

Problem 3: Sorting Complexity

An algorithm’s complexity refers to the number of computer operations executed when it is run. For this question you will analyze the complexity of the sorting algorithms that we’ve implemented. We will use the number of swaps made to sort the array as the measure the complexity of each sorting algorithm; the less swaps used to sort the array, the less complex the sorting algorithm is. Note that low values of complexity is desired as algorithms that require fewer operations will take less time to run. For each of the sorting algorithms you implemented, you will generate arrays of random numbers and record the number of swaps used by Bubble sort, and by PMerge sort. You will need to vary the sizes of the arrays generated, and plot, on the same graph, the complexity of both algorithms vs. the length of the array. The plot must be properly labeled with a title, axis labels, and a legend. a) First, we should solve the problem of generating random arrays. To do this, complete the function found at GenerateArray.m b) Use the GenerateArray function to implement the function in CompareSorts.m More detailed descriptions of these functions are found in the m‐files provided. Bubble sort is so named because of the way larger numbers ‘bubble’ to the end of the array. Merge sort gets its name from this crucial ‘merge’ step