# CS163_Week10 - CS163 Data structures Week 10 Sorting 8/2008...

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

CS163: Data structures Week 10: Sorting 8/2008

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

View Full Document
dbtien – Data structures CS163 today: Sorting Algorithms insertion sort selection sort exchange sort shell sort radix sort As we learn about each sorting algorithm, we will discuss its efficiency
dbtien – Data structures Sorting in General Sorting is the process that organizes collections of data into either ascending or descending order. Many of the applications you will deal with will require sorting; it is easier to understand data if it is organized numerically or alphabetically in order.

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

View Full Document
dbtien – Data structures Sorting in General As we found with the binary search, our data needs to be sorted to be able use more efficient methods of searching. There are two categories of sorting algs Internal sorting requires that all of your data fit into memory (an array or a LLL) External sorting is used when your data can't fit into memory all at once (maybe you have a very large database), and so sorting is done using disk files.
dbtien – Data structures Sorting in General Just like with searching, when we want to sort we need to pick from our data record the key to sort on (called the sort key). For example, if our records contain information about people, we might want to sort their names, id #s, or zip codes. Given a sorting algorithm, your entire table of information will be sorted based on only one field (the sort key).

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

View Full Document
dbtien – Data structures Sorting in General The efficiency of our algorithms is dependent on the number of comparisons we have to make with our keys. In addition, we will learn that sorting will also depend on how frequently we must move items around.
dbtien – Data structures Insertion Sort Think about a deck of cards for a moment. If you are dealt a hand of cards, imagine arranging the cards. One way to put your cards in order is to pick one card at a time and insert it into the proper position. The insertion sort acts just this way!

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

View Full Document
dbtien – Data structures Insertion Sort The insertion sort divides the data into a sorted and unsorted region. Initially, the entire list is unsorted. Then, at each step, the insertion sort takes the first item of the unsorted region and places it into its correct position in the sorted region.
dbtien – Data structures Insertion Sort Sort in class the following: 29, 10, 14, 37, 13, 12, 30, 20 Notice, the insertion sort uses the idea of keeping the first part of the list in correct order, once you've examined it. Now think about the first item in the list. Using this approach, it is always considered to be in order!

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

View Full Document
Insertion Sort Notice that with an insertion sort, even after most of the items have been sorted, the insertion of a later item may require that you move MANY of the numbers.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 44

CS163_Week10 - CS163 Data structures Week 10 Sorting 8/2008...

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

View Full Document
Ask a homework question - tutors are online