CS157 Sorting

- An array is a list of items (ints, floats, chars, structs, 2-D)
- Commonly, we want to search for an item in the list
- Is X in the list?
- What are the other values associated with X?
- E.g., look up userid “watti”; what is his GPA?

- Usually, we like to have our lists sorted in some way
- Ascending
- Descending
- By column
- By structure element

- Sorted lists are also helpful for more efficient search algorithms
- E.g., Binary search

- There are many ways to sort

The easiest algorithm is a Linear Search:

- Sequentially walk through the list.
- Nothing clever—just look
*everywhere*. - If the
*key*is found, then done. - Imagine searching through a phone book
that was
*not*in alphabetical order. - A linear search is generally the
*slowest*search.

#define SIZE 7 int arr[SIZE] = {1, 99, 25, 68, 92, 38, 74}; int key = 68; // searching for this int where = -1; // Where we found it for (int i=0; i<SIZE; i++) { if (arr[i] == key) { where = i; break; } } if (where == -1) printf("Sorry, %d not found in list\n", key); else printf("%d found at position %d\n", key, where);

68 found at position 3

Binary search, on the other hand, is smarter.
It takes advantage of the fact that the data that
you’re searching is *sorted* already.

- Consider the data in the middle.
- If that’s what we want, we’re done.
- Otherwise, focus on either the lower half or the upper half.

#define SIZE 7 int arr[SIZE] = {1, 25, 38, 68, 74, 92, 99}; int key = 68; // searching for this int low=0, mid, high=SIZE-1; while (low <= high) { mid = (low+high) / 2; if (arr[mid] == key) break; else if (arr[mid] > key) high = mid-1; else low = mid+1; } if (low <= high) printf("%d found at position %d\n", key, mid); else printf("Sorry, %d not found in list\n", key);

68 found at position 3

*Sorting*is the process of arranging a list of items in a particular order.- The sorting process is based on specific value(s)
- sorting a list of test scores in ascending numeric order

- sorting a list of people alphabetically by last name
- There are many algorithms, which vary in efficiency, for sorting a list of items
- We will examine three specific algorithms:
- Selection Sort
- Insertion Sort
- Bubble Sort

- The approach of Selection Sort:
- select a value and put it in its final place into the list
- repeat for all other values

- In more detail:
- find the smallest value in the list
- switch it with the value in the first position
- find the next smallest value in the list
- switch it with the value in the second position
- repeat until all values are in their proper places

Original: | 3 9 6 1 2 |

Smallest unsorted is 1; swap with first unsorted, 3: | 1 9 6 3 2 |

Smallest unsorted is 2; swap with first unsorted, 9: | 1 2 6 3 9 |

Smallest unsorted is 3; swap with first unsorted, 6: | 1 2 3 6 9 |

Smallest unsorted is 6; swap with first unsorted, 6: | 1 2 3 6 9 |

- The list has two parts: sorted on the left, unsorted on the right.
- Each time, the smallest remaining value is found and exchanged with the element in the “next” position to be filled
- There are 5 elements in the array, so only 4 exchanges need occur.
After that, the last element
*must*be the largest one.

- The processing of the selection sort
algorithm includes the
*swapping*of two values - Swapping requires three assignment statements and a temporary storage location:

temp = first; first = second; second = temp;

- The approach of Insertion Sort:
- pick any item and insert it into its proper place in a sorted sublist
- repeat until all items have been inserted

- In more detail:
- consider the first item to be a sorted sublist (of one item)
- insert the second item into the sorted sublist, shifting the first item as needed to make room to insert the new addition
- insert the third item into the sorted sublist (of two items), shifting items as necessary
- repeat until all values are inserted into their proper positions

Original: | 3 9 6 1 2 |

After inserting 9: | 3 9 6 1 2 |

After inserting 6: | 3 6 9 1 2 |

After inserting 1: | 1 3 6 9 2 |

After inserting 2: | 1 2 3 6 9 |

- The Selection and Insertion sort algorithms are similar in efficiency.
- They both have outer loops that scan all elements, and inner loops that compare the value of the outer loop with almost all values in the list
- Approximately n² number of comparisons are made to sort a list of size n
- We therefore say that these sorts are of order n²
- Other sorts are more efficient: order n·log n

- The approach of Bubble Sort:
- Walk through the list
- If the value at the given spot is in incorrect order with the next element, swap positions
- repeat as the list gets sorted from either smallest to biggest or vice versa

Bubbling largest element to the end of the list:

Original: | 3 9 6 1 2 |

After pass 1: | 3 6 1 2 9 |

After pass 2: | 3 1 2 6 9 |

After pass 3: | 1 2 3 6 9 |

After pass 4: | 1 2 3 6 9 |

Bubbling smallest element to the front of the list:

Original: | 3 9 6 1 2 |

After pass 1: | 1 3 9 6 2 |

After pass 2: | 1 2 3 9 6 |

After pass 3: | 1 2 3 6 9 |

After pass 4: | 1 2 3 6 9 |

#define SIZE 7 int arr[SIZE] = {1, 99, 25, 68, 92, 38, 74}; for (int i=1; i<SIZE; i++) for (int j=0; j<SIZE-i; j++) if (arr[j] > arr[j+1]) swap(arr, j, j+1); for (int i=0; i<SIZE; i++) printf("%d\n", arr[i]);

Modified: 2016-11-18T12:49 User: Edit History Source |
Apply to CSU |
Contact CSU |
Disclaimer |
Equal Opportunity Colorado State University, Fort Collins, CO 80523 USA © 2015 Colorado State University |