CSE100Lab11ManskiJosh

# CSE100Lab11ManskiJosh - Lab Section Name Lab 11 Sorting...

This preview shows pages 1–3. Sign up to view the full content.

Lab Section:________ Name:_____________________________________ Lab 11 Sorting Arrays Pre-Lab: Please read the pre-lab and answer the accompanying questions before your lab session. This lab will cover the following concepts: Why sorting arrays is a good idea How bubble sort works How selection sort works How to win a bet against your roommate Why we sort arrays: Arrays make it easy to store lots of data in a convenient, easily accessible form. Now that you've had a chance to work with arrays, you may have noticed that while it is fairly easy to put data in an array, knowing what you've put in there and accessing specific data quickly can be problematic. Up until now, we've just been sticking data in arrays without regard to what order the data goes in the array, and not caring whether the data is arranged in any kind of pattern. This comes with the unfortunate side effect that after populating an array with data, we aren't sure what we actually put in there, nor are we able to quickly and reliably access data in the array. This is where sorting comes in handy. Sorting an array is the process of arranging data in an array so that the data ends up in a certain order, usually following some formula or pattern (e.g., lowest to highest, shortest to longest). A sorting algorithm is a defined process by which an array can be sorted. After sorting an array using a sorting algorithm, the data will be structured in a way that makes it easier to access and examine the data, improving the efficiency of our program. Sorting algorithms can range in complexity from reasonably simple to unapproachably complex. In general, the harder an algorithm is to understand and implement, the better or faster it works. Unfortunately, this means that the "best" algorithms we have today make terrible teaching tools, because it's very difficult to truly understand what they do and how. So instead, we'll take a look at two simple sorting algorithms, bubble sort and selection sort. Bubble sort: The bubble sorting algorithm gets its inspiration from the bubbles you used to blow in your milk as a kid. Recall that as a layer of bubbles accumulated in your glass, the larger bubbles wandered up to the top of the layer, while the smaller bubbles remained closer to the milk. The bubble sort works in a similar manner. The bubble sort makes multiple passes through an array, each time shifting larger values toward the end of the array, keeping smaller values toward the beginning. After enough passes, everything gets shifted in to place, and the array is sorted smallest to largest. For example, let's say you have four puppies in your dorm room (they were cute so your CA let you keep them), and you just populated an array (let's call it puppyAges ) with their age in weeks, like this: int puppyAges[4] = {24, 12, 17, 8}; CSE 100 Lab 11 1 / 8

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This gives you an array that could be represented like this: puppyAges[0] puppyAges[1] puppyAges[2] puppyAges[3] 24 12 17 8 You'd like to sort the array so that you can quickly figure out the age of the youngest puppy (which will
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 8

CSE100Lab11ManskiJosh - Lab Section Name Lab 11 Sorting...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online