Sort - Computer Science

advertisement
Sorting
• A fundamental operation in computer science (many programs need to
sort as an intermediate step).
• Many sorting algorithms have been developed
• Choose a criteria which is used to order data
– Given a list of records that have keys, use those keys to define an ordering of
the items in the list
• To sort a set of data, the data have to be compared and moved as
necessary. Thus, measure the efficiency of a sorting algorithm by:
– Number of data comparisons it needs to perform
– Number of data movements it needs to perform
Elementary Sorting Algorithms
• given n items to sort
• There are a number of simple sorting
algorithms whose worst and average case
time complexity is quadratic (i.e., O(n2))
– Insertion sort
– Selection sort
– Bubble sort
Insertion Sort
• Given an array of data items:
– The insertion sort algorithm views the array as having
a sorted side and an unsorted side
– The sorted side starts with the first element, which is
not necessarily the smallest element
– The sorted side grows by taking the front element
from the unsorted side and inserting it in the place
that keeps the sorted side arranged from small to
large
– In some cases there is no need to move the new
inserted item
The Insertion Sort Algorithm
template <class T>
void insertionsort(T data[], int n){
for(int i=1, j; i<n; i++){
T tmp =data[i]; //take next key from unsorted part of array
for (j =i; j>0 && tmp < data[j-1]; j--) //shift data down to make
room for inserting tmp if necessary
data[j] = data[j-1];
data[j] = tmp; //insert in appropriate location in sorted part of array
}
}
Complexity of Insertion Sort
• Best case O(n)
• Worst and average case O(n2)
Selection Sort
• Basic idea:
– Repeatedly select the smallest element and move
this element to the front of the unsorted side
selectionsort
template<class T>
void selectionsort(T data[], int n) {
int j;
for (int i = 0, least, j; i < n-1; i++) {
//find the index of the smallest element in the unsorted part
of array
}
}
for (j = i+1, least = i; j < n; j++)
if (data[j] < data[least])
least = j;
//put the ith smallest to the i-1th position
swap(data[least],data[i]);
Selection Sort (cont’d)
• Given an array of integers:
– Start by finding the smallest element
• Swap the smallest entry with the first element
• Part of the array is sorted
– Find the smallest element in the unsorted side
• Swap with the front of the unsorted side
• The size of the sorted side is increased by one element
– Continue until the unsorted side has just one
number.
selectionsort
• complexity
BubbleSort
• Given an array of integers:
– Scan the array from right to left
– Look at pairs of elements (adjacent elements) in the
array and swap their order if needed (i.e., if the left
element is larger than the right element)
– Repeatedly scan the array from right to left looking at
pairs of elements and swapping their order if needed
• In this way, the smallest element is “bubbled” all the way to
the first position of the array
– Scan the array again from right to left to “bubble” the
second smallest element to the second of the array
– Continue scanning until done
bubblesort
template<class T>
void bubblesort(T data[], int n) {
// n passes, each pass ”bubble” one element
for (int i = 0; i < n-1; i++)
//keep comparing adjacent elements
for (int j = n-1; j > i; --j)
if (data[j] < data[j-1])
swap(data[j],data[j-1]);
}
BubbleSort
• complexity
Elementary Sorting Algorithms
• Selection Sort, Insertion Sort, and Bubble Sort all
have a worst-case time of O(n2), making them
impractical for large arrays
• But they are easy to program, easy to debug
• Insertion Sort also has good performance when the
array is nearly sorted to begin with
• But more sophisticated algorithms are needed when
good performance is needed in all cases for large
arrays
– Heap Sort
– Quick Sort
– Merge Sort
Download