







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of various sorting algorithms, including bubble sort, insertion sort, selection sort, shell sort, merge sort, and quicksort. It discusses the efficiency and running time of each algorithm, comparing their performance for different sizes of data collections. The document also includes figures illustrating the steps involved in bubble sort, insertion sort, selection sort, and merge sort. It is a useful resource for understanding the principles and trade-offs of different sorting techniques, highlighting their advantages and disadvantages in various scenarios. The document emphasizes the importance of choosing the right sorting algorithm based on the size and characteristics of the data being processed, and it provides insights into the complexities and practical considerations of sorting large datasets.
Typology: Summaries
1 / 13
This page cannot be seen from the preview
Don't miss anything!








Data Structures and Algorithms
Sorting
Sorting is the process of placing elements from a collection in some kind of order. For example, a list of words could be sorted alphabetically or by length. A list of cities could be sorted by population, by area, or by zip code.
There are many, many sorting algorithms that have been developed and analyzed. This suggests that sorting is an important area of study in computer science. Sorting a large number of items can take a substantial amount of computing resources. Like searching, the efficiency of a sorting algorithm is related to the number of items being processed. For small collections, a complex sorting method may be more trouble than it is worth. The overhead may be too high. On the other hand, for larger collections, we want to take advantage of as many improvements as possible. We will discuss several sorting techniques and compare them with respect to their running time.
(Aho, A. V., Hopcroft, J. E., & Ullman, J. D. (2009). Data Structures and algorithms. Delhi: Dorling Kindersly.)
In this chapter, we shall present the principal internal sorting algorithms. The simplest algorithms usually take O ( n 2) time to sort n objects and are only useful for sorting short lists. One of the most popular sorting algorithms is quicksort, which takes O ( n log n ) time on average. Quicksort works well for most common applications, although, in the worst case, it can take O ( n 2) time. There are other methods, such as heapsort and mergesort, that take O ( n log n ) time in the worst case, although their average case behavior may not be quite as good as that of quicksort. Mergesort, however, is well-suited for external sorting.
The Bubble Sort
Perhaps the simplest sorting method one can devise is an algorithm called "bubblesort." The basic idea behind bubblesort is to imagine that the records to be sorted are kept in an array held vertically. The records with low key values are "light" and bubble up to the top. We make multiple passes over the array, from bottom to top. As we go, if two adjacent elements are out of order, that is, if the "lighter" one is below, we reverse them. The effect of this operation is that on the first pass the "lightest" record, that is, the record with the lowest key value, rises all the way to the top. On the second pass, the second lowest key rises to the second position, and so on. We need not, on pass two, try to bubble up to position one, because we know the lowest key already resides there.
Data Structures and Algorithms
Figure 1- First Pass of the Bubblesort
The shaded items are being compared to see if they are out of order. If there are n items in the list, then there are n −1. The pairs of items that need to be compared on the first pass. It is important to note that once the largest value in the list is part of a pair, it will continually be moved along until the pass is complete.
At the start of the second pass, the largest value is now in place. There are n −1. The items left to sort, meaning that there will be n −2 pairs. Since each pass places the next largest value in place, the total number of passes necessary will be n −1. After completing the n −1.
(1) for i := 1 to n -1 do
(2) for j := n downto i +1 do
(3) if A [ j ]. key < A [ j -1]. key
then
(4) swap ( A [ j ], A [ j -1])
(Aho, A. V., Hopcroft, J. E., & Ullman, J. D. (2009). Data Structures and algorithms. Delhi: Dorling Kindersly.)
Data Structures and Algorithms
We begin by assuming that a list with one item (position 0) is already sorted. On each pass, one for each item 1 through n −1, the current item is checked against those in the already sorted sublist. As we look back into the already sorted sublist, we shift those items that are greater to the right. When we reach a smaller item or the end of the sublist, the current item can be inserted.
(Aho, A. V., Hopcroft, J. E., & Ullman, J. D. (2009). Data Structures and algorithms. Delhi: Dorling Kindersly.)
The table below shows the fifth pass in detail. At this point in the algorithm, a sorted sublist of five items consisting of 17, 26, 54, 77, and 93 exists. We want to insert 31 back into the already sorted items. The first comparison against 93 causes 93 to be shifted to the right. 77 and 54 are also shifted. When the item 26 is encountered, the shifting process stops and 31 is placed in the open position. Now we have a sorted sublist of six items.
Figure 3 - 5th Element Passing
Selection sort
The selection sort improves on the bubble sort by making only one exchange for every pass through the list. In order to do this, a selection sort looks for the largest value as it makes a pass and, after completing the pass, places it in the proper location. As with a bubble sort, after the first pass, the largest item is in the correct place. After the second pass, the next largest is in place. This process continues and requires n −1 passes to sort n items, since the final item must be in place after the ( n −1) st pass
(Aho, A. V., Hopcroft, J. E., & Ullman, J. D. (2009). Data Structures and algorithms. Delhi: Dorling Kindersly.)
Data Structures and Algorithms
Figure 4 shows the entire sorting process. On each pass, the largest remaining item is selected and then placed in its proper location. The first pass places 93, the second pass places 77, the third places 55, and so on.
Figure 4 - Selection Sort
Shell Sort
The shell sort , sometimes called the “diminishing increment sort,” improves on the insertion sort by breaking the original list into a number of smaller sublists, each of which is sorted using an insertion sort. The unique way that these sublists are chosen is the key to the shell sort. Instead of breaking the list into sublists of contiguous items, the shell sort uses an increment i, sometimes called the gap , to create a sublist by choosing all items that are i items apart.
Data Structures and Algorithms
Figure 7 - Final Insertion sort an increment of one
Figure 8 – First Sub list using this increment
Data Structures and Algorithms
We said earlier that the way in which the increments are chosen is the unique feature of the shell sort. In this case, we begin with n 2 sublists. On the next pass, n 4 sublists are sorted. Eventually, a single list is sorted with the basic insertion sort.
(Aho, A. V., Hopcroft, J. E., & Ullman, J. D. (2009). Data Structures and algorithms. Delhi: Dorling Kindersly.)
Merge Sort
We now turn our attention to using a divide and conquer strategy as a way to improve the performance of sorting algorithms. The first algorithm we will study is the merge sort. Merge sort is a recursive algorithm that continually splits a list in half. If the list is empty or has one item, it is sorted by definition (the base case). If the list has more than one item, we split the list and recursively invoke a merge sort on both halves. Once the two halves are sorted, the fundamental operation, called a merge , is performed. Merging is the process of taking two smaller sorted lists and combining them together into a single, sorted, new list.
Figure 9 - List split by mergesort
Data Structures and Algorithms
Quick Sort
The quick sort uses divide and conquer to gain the same advantages as the merge sort, while not using additional storage. As a trade-off, however, it is possible that the list may not be divided in half. When this happens, we will see that performance is diminished.
A quick sort first selects a value, which is called the pivot value. Although there are many different ways to choose the pivot value, we will simply use the first item in the list. The role of the pivot value is to assist with splitting the list. The actual position where the pivot value belongs in the final sorted list, commonly called the split point , will be used to divide the list for subsequent calls to the quick sort.
Figure 11 shows that 54 will serve as our first pivot value. The partition process will happen next. It will find the split point and at the same time move other items to the appropriate side of the list, either less than or greater than the pivot value.
Figure 11 - 54 will serve as as pivot value
Partitioning begins by locating two position markers—let’s call them leftmark and rightmark—at the beginning and end of the remaining items in the list (positions 1 and 8). The goal of the partition process is to move items that are on the wrong side with respect to the pivot value while also converging on the split point.
Data Structures and Algorithms
Figure 12 - Quick sort
We begin by incrementing leftmark until we locate a value that is greater than the pivot value. We then decrement rightmark until we find a value that is less than the pivot value. At this point we have discovered two items that are out of place with respect to the eventual split point. For our example, this occurs at 93 and 20. Now we can exchange these two items and then repeat the process again.
At the point where rightmark becomes less than leftmark, we stop. The position of rightmark is now the split point. The pivot value can be exchanged with the contents of the split point and the pivot value is now in place. In addition, all the items to the left of the split point are less than the pivot value, and all the items to the right of the split point are greater than the pivot value. The list can now be divided at the split point and the quick sort can be invoked recursively on the two halves.
(Miller, B., & Ranum, D. (n.d.). Problem Solving with Algorithms and Data Structures using Python)
Data Structures and Algorithms
References