Top Banner
Title: Sorting Objectives: 1. To explain the concept of sorting techniques. 2. To formulate suitable sorting algorithms to increase data performance in term of memory and run time efficiency of the given programming problem. 3. To investigate and analyze the efficiency of each sorting technique. 4. To discuss the result from this experiment and present technical report. 5. To complete every tasks in this experiment effectively as individual or in group. Theory: Sorting algorithm In computer science and mathematics, a sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important to optimizing the use of other algorithms (such as search and merge algorithms) that require sorted lists to work correctly; it is also often useful for canonicalizing data and for producing human-readable output. More formally, the output must satisfy two conditions: 1. The output is in nondecreasing order (each element is no smaller than the previous element according to the desired total order); 2. The output is a permutation, or reordering, of the input. Since the dawn of computing, the sorting problem has attracted a great deal of research, perhaps due to the complexity of solving it efficiently despite its simple, familiar statement. For example, bubble sort was analyzed as early as 1956. [1] Although many consider it a solved problem, useful new sorting algorithms are still being invented (for example, library sort was first published in 2004). Sorting algorithms are prevalent in introductory computer science classes, where the abundance of algorithms for the problem provides a gentle introduction to a variety of core algorithm concepts, such as big O notation, divide and conquer algorithms, data structures, randomized algorithms, best, worst and average case analysis, time-space tradeoffs, and lower bounds.[1] 1
22
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Sorting

Title: SortingObjectives: 1. To explain the concept of sorting techniques. 2. To formulate suitable sorting algorithms to increase data performance in term of memory and run time

efficiency of the given programming problem. 3. To investigate and analyze the efficiency of each sorting technique. 4. To discuss the result from this experiment and present technical report. 5. To complete every tasks in this experiment effectively as individual or in group.

Theory:

Sorting algorithmIn computer science and mathematics, a sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important to optimizing the use of other algorithms (such as search and merge algorithms) that require sorted lists to work correctly; it is also often useful for canonicalizing data and for producing human-readable output. More formally, the output must satisfy two conditions:

1. The output is in nondecreasing order (each element is no smaller than the previous element according to the desired total order);

2. The output is a permutation, or reordering, of the input.Since the dawn of computing, the sorting problem has attracted a great deal of research, perhaps due to the complexity of solving it efficiently despite its simple, familiar statement. For example, bubble sort was analyzed as early as 1956.[1] Although many consider it a solved problem, useful new sorting algorithms are still being invented (for example, library sort was first published in 2004). Sorting algorithms are prevalent in introductory computer science classes, where the abundance of algorithms for the problem provides a gentle introduction to a variety of core algorithm concepts, such as big O notation, divide and conquer algorithms, data structures, randomized algorithms, best, worst and average case analysis, time-space tradeoffs, and lower bounds.[1]

ClassificationSorting algorithms used in computer science are often classified by: [1] Computational complexity (worst, average and best behaviour) of element comparisons in terms of the

size of the list . For typical sorting algorithms good behavior is and bad behavior is

. (See Big O notation.) Ideal behavior for a sort is , but this is not possible in the average case. Comparison-based sorting algorithms, which evaluate the elements of the list via an abstract key

comparison operation, need at least comparisons for most inputs. Computational complexity of swaps (for "in place" algorithms). Memory usage (and use of other computer resources). In particular, some sorting algorithms are "in

place". This means that they need only or memory beyond the items being sorted and they don't need to create auxiliary locations for data to be temporarily stored, as in other sorting algorithms.

1

Page 2: Sorting

Recursion. Some algorithms are either recursive or non-recursive, while others may be both (e.g., merge sort).

Stability: stable sorting algorithms maintain the relative order of records with equal keys (i.e., values). See below for more information.

Whether or not they are a comparison sort. A comparison sort examines the data only by comparing two elements with a comparison operator.

General method: insertion, exchange, selection, merging, etc.. Exchange sorts include bubble sort and quicksort. Selection sorts include shaker sort and heapsort.

Adaptability: Whether or not the presortedness of the input affects the running time. Algorithms that take this into account are known to be adaptive.

Comparison of algorithmsn this table, n is the number of records to be sorted. The columns "Average" and "Worst" give the time complexity in each case, under the assumption that the length of each key is constant, and that therefore all comparisons, swaps, and other needed operations can proceed in constant time. "Memory" denotes the amount of auxiliary storage needed beyond that used by the list itself, under the same assumption. These are all comparison sorts. The run-time and the memory of algorithms could be measured using various notations like theta, sigma, Big-O, small-o, etc. The memory and the run-times below are applicable for all the 5 notations. [1]

2

Page 3: Sorting

Table 1: Comparison of various sorting techniques. [1]

Equipment:(1) Computer (Laptop or deskstop)(2) Any editor for C++ programming language installed in computer(3) Fundamental C++ programming skill

Task:Task 1: By using some figures and example, illustrate the sorting process of the following algorithms:a) Bubble sort b) Selection sort c) Insertion sort d) Quick sort

3

Page 4: Sorting

(a) Bubble SortIn the bubble sort, as elements are sorted they gradually "bubble" (or rise) to their proper location in the array, like bubbles rising in a glass of soda.  The bubble sort repeatedly compares adjacent elements of an array.  The first and second elements are compared and swapped if out of order.  Then the second and third elements are compared and swapped if out of order.  This sorting process continues until the last two elements of the array are compared and swapped if out of order.  

Diagram 1: Example of Bubble sort

When this first pass through the array is complete, the bubble sort returns to elements one and two and starts the process all over again.  So, when does it stop?  The bubble sort knows that it is finished when it examines the entire array and no "swaps" are needed (thus the list is in proper order).  The bubble sort keeps track of occurring swaps by the use of a flag.

The diagram below follows an array of numbers before, during, and after a bubble sort for ascending order.  A "pass" is defined as one full trip through the array comparing and if necessary, swapping, adjacent elements.  Several passes have to be made through the array before it is finally sorted.  [2]For example:

4

Page 5: Sorting

Diagram 2: Bubble sort step by step solution [3]

The bubble sort is an easy algorithm to program, but it is slower than many other sorts.   With a bubble sort, it is always necessary to make one final "pass" through the array to check to see that no swaps are made to ensure that the process is finished.  In actuality, the process is finished before this last pass is made. [2]

(b) Selection SortThe selection sort is a combination of searching and sorting. During each pass, the unsorted element with the smallest (or largest) value is moved to its proper position in the array. The number of times the sort passes

5

Page 6: Sorting

through the array is one less than the number of items in the array.  In the selection sort, the inner loop finds the next smallest (or largest) value and the outer loop places that value into its proper location. [4]For example:

Diagram 3: Selection sort step by step solution [5]

6

Page 7: Sorting

(c) Insertion SortThe insertion sort, unlike the other sorts, passes through the array only once.  The insertion sort is commonly compared to organizing a handful of playing cards.  You pick up the random cards one at a time.  As you pick up each card, you insert it into its correct position in your hand of organized cards. 

Diagram 4: Insertion sort is like sorting cards

The insertion sort splits an array into two sub-arrays. The first sub-array (such as the cards in your hand) is sorted and increases in size as the sort continues. The second sub-array (such as the cards to be picked up) is unsorted, contains all the elements to yet be inserted into the first sub-array, and decreases in size as the sort continues. [6]Let's look at example using the insertion sort for ascending order:

7

Page 8: Sorting

Diagram 5: Insertion sort step by step solution [7]

8

Page 9: Sorting

(d) Quick SortQuicksort is a fast sorting algorithm, which is used not only for educational purposes, but widely applied in practice. On the average, it has O(n log n) complexity, making quicksort suitable for sorting big data volumes. The idea of the algorithm is quite simple and once you realize it, you can write quicksort as fast as bubble sort. The divide-and-conquer strategy is used in quicksort. Below the recursion step is described: 1. Choose a pivot value. We take the value of the middle element as pivot value, but it can be any value,

which is in range of sorted values, even if it doesn't present in the array.2. Partition. Rearrange elements in such a way, that all elements which are lesser than the pivot go to the

left part of the array and all elements greater than the pivot, go to the right part of the array. Values equal to the pivot can stay in any part of the array. Notice, that array may be divided in non-equal parts.

3. Sort both parts. Apply quicksort algorithm recursively to the left and the right parts.There are two indices i and j and at the very beginning of the partition algorithm i points to the first element in the array and j points to the last one. Then algorithm moves i forward, until an element with value greater or equal to the pivot is found. Index j is moved backward, until an element with value lesser or equal to the pivot is found. If i ≤ j then they are swapped and i steps to the next position (i + 1), j steps to the previous one (j - 1). Algorithm stops, when i becomes greater than j.After partition, all values before i-th element are less or equal than the pivot and all values after j-th element are greater or equal to the pivot. [8]

9

Page 10: Sorting

Diagram 6: Quick sort step by step solution [8]

10

Page 11: Sorting

Task 2: Write a program in C++ that applies different sorting techniques to solve integer sorting problem. Your program must operate as follows:

Diagram 7: Example output of Task 2

Programming code:

Coding part 1: Preprocessor directive and function prototype

11

Select (1) for ascending, (2) for descending order: 2 Enter list of size: 6 Enter number: 4 Enter number: 2 Enter number: 3 Enter number: 1 Enter number: 6 Enter number: 5 Sorting techniques: Select (1) for Bubble Select (2) for Selection Select (3) for Insertion Select (4) for Quicksort Your choice: 4 Sorted list for Quick sort: 6 5 4 3 2 1

Page 12: Sorting

Coding part 2: Main function

12

Page 13: Sorting

Coding part 3: Enter number function

Coding part 4: Bubble sort function

13

Page 14: Sorting

Coding part 5: Selection sort function

Coding part 6: Insertion sort function

14

Page 15: Sorting

Coding part 7: Quick sort function and partition function used under quick sort technique

Coding part 8: Display array function

Results:15

Page 16: Sorting

Diagram 8: Result for ascending order bubble sort

Diagram 9: Result for descending order bubble sort

16

Page 17: Sorting

Diagram 10: Result for ascending order selection sort

Diagram 11: Result for descending order selection sort

17

Page 18: Sorting

Diagram 12: Result for ascending order insertion sort

Diagram 13: Result for descending order insertion sort

18

Page 19: Sorting

Diagram 14: Result for ascending order quick sort

Diagram 15: Result for descending order quick sort

19

Page 20: Sorting

Discussion:(1) Sorting is the process of sequencing or arranging a list of data items according to some order whether

ascending or descending.(2) The main purpose of sorting is to improve the search or access time. Without sorting, it will generally

take a longer time to acess information in a file or database especially if the database is large.(3) In this 4 sorting techniques used, quick sort has the highest efficiency where worse case having Big-O

notation O(n2) and average case of O(nlogn). Although the result can not prove the efficiency of the technique, but if the code is broke down into pieces and let the system display the array every time a changes is made, quick sort has less line compare to the other 3.

(4) All the rest, i.e. insertion sort, selection sort and bubble sort has Big-O notation of O(n2). It is less efficient that quick sort because no matter on what case (worst, average or best), the techniques need to run the program n-1 time before it stop although the list might have sorted out.

Conclusion:(1) The sorting algorithm using 4 different sort i.e. bubble sort, selection sort, insertion sort, quick sort is

understand and coding is done using all the 4 sorting techniques.

Reference: [1] http://en.wikipedia.org/wiki/Sorting_algorithm, adapted on 30th September 2010.[2] http://mathbits.com/mathbits/compsci/arrays/bubble.htm, adapted on 30th September 2010.[3] http://www.algolist.net/Algorithms/Sorting/Bubble_sort, adapted on 30th September 2010.[4] http://mathbits.com/mathbits/compsci/arrays/Selection.htm, adapted on 30th September 2010.[5] http://www.algolist.net/Algorithms/Sorting/Selection_sort, adapted on 30th September 2010.[6] http://mathbits.com/mathbits/compsci/arrays/Insertion.htm, adapted on 30th September 2010.[7] http://www.algolist.net/Algorithms/Sorting/Insertion_sort, adapted on 30th September 2010.[8] http://www.algolist.net/Algorithms/Sorting/Quicksort, adapted on 30th September 2010.

20