Sorting by Amir Kamil

# Sorting by Amir Kamil - CS61B Spring 2003 UC Berkeley...

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

CS61B, Spring 2003 Discussion #15 Amir Kamil UC Berkeley 4/28/03 Topics: Sorting 1 Sorting The topic of sorting really requires no introduction. We start with an unsorted sequence, and want a sorted sequence as the result. We begin by discussing simple but slow sorts, and move on to quicker but more complicated algorithms. We will neglect the possibility of duplicate elements for most of the sorts, and we will use integers as the keys in all of them. 1.1 Selection Sort An obvious sorting algorithm is to repeatedly ﬁnd and remove the smallest item in a set and store that item in a result set. This is a type of selection sort . The actual algorithm we will use is in-place , so the result set is the start set. We have to keep track of what part of the set is sorted and what part isn’t. Instead of removing the smallest element, we swap it with the ﬁrst element in the unsorted part, and then extend the sorted part to include the smallest element. A Java implementation is as follows: void selectionSort(int[] x) { int tmp, minpos; for (int i = 0; i < x.length; i++) { // i is the start of the unsorted part // find the smallest remaining element minpos = i; for (int j = i+1; j < x.length; j++) { if (x[j] < x[minpos]) { minpos = j; } } // swap the smallest element with the first unsorted element tmp = x[i]; x[i] = x[minpos]; x[minpos] = tmp; } } Selection sort is a particulary slow algorithm. Finding the smallest remaining unsorted element takes n 2 time on average, and must be done n times, for an O ( n 2 ) running time. The running time is always the same regardless of the distribution of values in the input. 1.2 Insertion Sort Another obvious way to sort a set is to start with an empty result set and repeatedly remove the ﬁrst element in the original set and insert it into the proper location in the result set. This can be easily accomplished using linked lists. A pseudocode algorithm: LinkedList insertionSort(LinkedList x) { Create a new LinkedList y; while x is not empty do: temp = x.remove(0); Find the first element in y that is greater than temp, call it z; if z exists then: Insert x before z; else: 1

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

View Full Document
Figure 1: Selection sort applied to a sequence of integers. 2
Insert x at the end of y; fi; od; return y; } On average, ﬁnding the greater element in the result list takes n 2 time, and must be done n times, for a total of O ( n 2 ) running time. However, in some cases such as a reverse sorted list, ﬁnding the element is constant, so the running time reduces to linear. Modiﬁcations to the algorithm can be made such that sorted rather than reverse sorted lists can be linearly resorted. 1.3

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 8

Sorting by Amir Kamil - CS61B Spring 2003 UC Berkeley...

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

View Full Document
Ask a homework question - tutors are online