This action might not be possible to undo. Are you sure you want to continue?

# International academy for engineering and media science

FACULTY OF ENGINEERING ELECTRICAL ENGINEERING DEPARTMENT Communication major.

Report Sorting algorithms. Data structures & Algorithms.

Supervisor: D. Mostafa El sayed

Prepared by:

NADA TAWFICK

.....Quick sort comparison between sorting algorithms 5 ................... 4 2................... .. ........................... ...................................... 3 Popular sorting algorithms .............................................. .......................................................... .............. .7 8 LIST OF TABLES Table ( 1):Comparison of sorting algorithms .............................. 7 ................................. 4 3............1): Insertion sort....................... .......................................... ... .......... 5 Figure (2...........Insertion sort 4............. ...................................... ............................. ........................... 6 Figure (3... ...................................................1): Shell sort............ ............ ..... 4 1...... ...... Selection sort..............................Shell sort 5.................................................................... .................................... ....................6 . 2 Classification of sorting algorithms ................ ....... .............Table of Contents Abstract .... 8 Table (2): Sorting timing.1): Quick sort ................. ............ ....Bubble sort ....... ......... ........ ........................... 8 LIST OF FIGURES Figure (1....................................................................

library sort was first published in 2004). data structures. 1-The output is in nondecreasing order (each element is no smaller than the previous element according to the desired total order . randomized algorithms.Abstract In computer science. Sorting algorithms are prevalent in introductory computer science classes. and lower bounds. familiar statement. 2-The output is a permutation. useful new sorting algorithms are still being invented (for example. More formally. time-space tradeoffs. a sorting algorithm is an algorithm that puts elements of a list in a certain order. Since the dawn of computing. or reordering. bubble sort was analyzed as early as 1956. worst and average case analysis. where the abundance of algorithms for the problem provides a gentle introduction to a varie ty of core algorithm concepts. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) that require sorted lists to work correctly. The most-used orders are numerical order . divide and conquer algorithms. the output must satisfy two conditions. of the input. For example.Although many consider it a solved problem. the sorting problem h as attracted a great deal of research. perhaps due to the complexity of solving it efficiently despite its simple. best. it is also often useful for canonicalizing data and for producing human-readable output. such as big O notation. .

Exchange sorts include bubble sort and quicksort. Classification: Sorting algorithms used in computer science are often classified by : o Computational complexity (worst. . exchange. merging. In particular. etc.g. average and best behaviour) of element comparisons in terms of the size of the list (n). values). while others may be both (e. while others need to create auxiliary locations for data to be temporarily stored . merge sort).2 Sorting algorithms: The methods followed in arranging a set of data items into a sequence according to precise rules. such that only O(1) or O(log n) memory is needed beyond the items being sorted. A compari son sort examines the data only by comparing two elements with a comparison operator. Sort algorithms which only use an abstract key comparison operation always need (n log n) comparisons in the worst case. Some algorithms are either recursive or non recursive. o Stability: stable sorting algorithms maintain the relative order of records with equal keys(i. selection.e. o Computational complexity of swaps (for "in place" algorithms) . o General method: insertion. o Recursion.. (See Big O notation) Ideal behavior for a sort is O(n). Selection sorts include shaker sort and heapsort. For typical sorting algorithms good behavior is O(n log n) and bad behavior is (n²). o Memory usage (and use of other computer resources). some sorting algorithms are "in place". o Whether or not they are a comparison sort..

works by inverting the ordering criteria and the pass direction on alternating passes. Selection sort requires (n . It continues doing this for each pair of adjacent elements to the end of the data set.1) / 2 = (n2) comparisons. + 2 + 1 = n(n . However. A slightly better variant. Selection sort requires (n . Selection sort is unique compared t o almost any other algorithm in that its running time is not affected by the prior ordering of the list: it performs the same number of operations because of its simple structure. and if the first is greater than the second. The algorithm starts at the beginning of the data set.2) + . 2-Selection sort Selection sort is a simple sorting algorithm that improves on the performance of bubble sort. then finding the second smallest element by scanning the remaining elements. Its average case and worst case are both O(n²). Thus it can be very attractive if writes are the most expensive operation. but otherwise selection sort will usually be outperformed by insertion sort or the more complicated algorithms.3 Popular sorting algorithms. While simple. repeating until no swaps have occurred on the last pass. cocktail sort. It then starts again with the first two elements. . and so on. 1-Bubble sort Bubble sort is a straightforward and simplistic method of sorting data that is used in computer science education. It compares the first two elements. It works by first finding the smallest element using a linear scan and swapping it into the first position in the list..1) swaps and hence (n) memory writes. it swaps them.1) + (n .. this algorithm is highly inefficient and is rarely used except in education.

the source list and the list into which sorted items are inserted. requiring shifting all following elements over by one. The simplest implementation of this requires two list structures . To save memor y. the new list and the remaining elements can share the array's space. . and often is used as part of more sophisticated algorithms.it inserts each item into its proper place in the final list. It works by taking elements from the list one by one and inserting them in their correct position into a new sorted list. Figure 1-1: insertion sort . though it has more constraints. Shell sort (see below) is a variant of insertion sort that is more efficient for larger lists.4 3-Insertion sort Insertion sort is a simple sorting algorithm that is relatively efficient for small lists and mostly-sorted lists. The insertion sort works just like its name suggests . This method is much more efficient than the bubble sort. In arrays. but insertion is expensive. most implementations use an in-place sort that works by moving the current item past the already sorted items and repeatedly swapping it with the preceding item until it is in place.

It improves upon bubble sort and insertion sort by moving out of order elements more than one position at a time. 6 . it is one of the fastest algorithms for sorting small numbers of elements (sets with less than 1000 or so elements). Another advantage of this algorithm is that it requires relatively small amounts of memory. figure 2-1:shell sort.5 4-Shell sort Shell sort was invented by Donald Shell in 1959. One implementation can be described as arranging the data sequence in a two-dimensional array and then sorting the columns of the array using insertion sort. Although this method is inefficient for large data sets.

Together with its modest O(log n) space usage. we choose an element. Efficient implementations of quicksort (with in-place partitioning) are typically unstable sorts and somewhat complex. move all smaller elements before the pivot.5-Quicksort Quicksort is a divide and conquer algorithm which relies on a partition operation: to partition an array. called a pivot. The most complex issue in quicksort is choosing a good pivot element. but are among the fas test sorting algorithms in practice. available in many standard libraries. . We then recursively sort the lesser and greater sublists. and move all greater elements after it. but if at each step we choose the median as the pivot then it works in O(n log n) Figure 3-1: Quick sort . consistently poor choices of pivots can result in drastically slower (O(n²)) performance. This can be done efficiently in linear time and in -place. this makes quicksort one of the most popular sorting algorithms.

The time required to sort a randomly ordered dataset is shown in Table 1-2 Simplicity. Both insertion sort and shell sort are in. There are several factors that influence the choice of a sorting algorithm: Stable sort. The number of statements required for each algorithm may be found in Table 11Simpler algorithms result in fewer programming errors. Insertion sort is the only algorithm covered that is stable. and therefore is not an in-place sort. Space. Time. Table (2) sort timings. . Recall that a stable sort will leave identical keys in the same relative position in the sorted output. Quicksort requires stack space for recursion. . The time required to sort a dataset can easily become astronomical Table 12shows the relative timings for each method. Table( 1) comparison of sorting methods. Tinkering with the algorithm considerably reduced the amount of time required. shell sort. and quicksort.7 Comparison In this section we will compare the sorting algorithms covered: insertion sort.place sorts. An in-place sort does not require any extra space to accomplish its task.

8 .