SlideShare a Scribd company logo
1 of 38
Download to read offline
Basic Comparison Sort in Data Structures

                                 Dr.Haitham A. El-Ghareeb

                                  Information Systems Department
                           Faculty of Computers and Information Sciences
                                        Mansoura University
                                       helghareeb@gmail.com


                                     November 11, 2012




Dr.Haitham A. El-Ghareeb (CIS)     Data Structures and Algorithms - 2012   November 11, 2012   1 / 38
Sorting
Sorting is any process of arranging items in some sequence and/or in
different sets, and accordingly, it has two common, yet distinct meanings:
      Ordering: arranging items of the same kind, class, nature, etc. in
      some ordered sequence,
      Categorizing: grouping and labeling items with similar properties
      together (by sorts).




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   2 / 38
Order Theory
Order theory is a branch of mathematics which investigates our intuitive
notion of order using binary relations. It provides a formal framework for
describing statements such as ”this is less than that” or ”this precedes
that”.




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   3 / 38
Sorting Information / Data
Sorting n-tuples (depending on context also called e.g. records consisting
of fields) can be done based on one or more of its components. More
generally objects can be sorted based on a property. Such a component or
property is called a sort key. For example, the items are books, the sort
key is the title, subject or author, and the order is alphabetical.




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   4 / 38
Weak Order
If the sort key values are totally ordered, the sort key defines a weak order
of the items: items with the same sort key are equivalent with respect to
sorting. If different items have different sort key values then this defines a
unique order of the items.




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   5 / 38
Standard Order
A standard order is often called ascending (corresponding to the fact that
the standard order of numbers is ascending, i.e. A to Z, 0 to 9), the
reverse order descending (Z to A, 9 to 0). For dates/times ascending
means that earlier values precede later ones e.g. 1/1/2012 will sort ahead
of 1/1/2013.




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   6 / 38
Sorting in Computer Science
     Sorting is one of the most extensively researched subjects because of
     the need to speed up the operation on thousands or millions of
     records during a search operation. The main purpose of sorting
     information is to optimise its usefulness for specific tasks.
     Most common sorting purposes are Name, by Location and by Time
     (these are actually special cases of category and hierarchy). Together
     these give the acronym LATCH (Location, Alphabetical, Time,
     Category, Hierarchy) and can be used to describe just about every
     type of ordered information.




Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   7 / 38
Opposite of Sorting
The opposite of sorting, rearranging a sequence of items in a random or
meaningless order, is called Shuffling.




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   8 / 38
Sorting Algorithm
     In computer science, 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
     (generalization of Alphabetical order).
     Efficient sorting is important for 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.




Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   9 / 38
Sorting Algorithm (Cont.)
      More formally, the output must satisfy two conditions:
              The output is in nondecreasing order (each element is no smaller than
              the previous element according to the desired total order);
              The output is a permutation (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.
      Although many consider it a solved problem, useful new sorting
      algorithms are still being invented (for example, library sort was first
      published in 2006).




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   10 / 38
Sorting Algorithms Classification
Sorting algorithms used in computer science are often classified by:
     Computational complexity (worst, average and best behavior) of
     element comparisons in terms of the size of the list (n). For typical
     sorting algorithms good behavior is O(n log n) and bad behavior is
     O(n2). Ideal behavior for a sort is O(n), 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 O(n log n) 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”. Strictly, an in place sort needs
     only O(1) memory beyond the items being sorted; sometimes
     O(log(n)) additional memory is considered ”in place”.
     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). Whether or not they11, 2012
 Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November
                                                                               are a   11 / 38
Comparison Sort
     A comparison sort is a type of sorting algorithm that only reads the
     list elements through a single abstract comparison operation (often a
     ”less than or equal to” operator) that determines which of two
     elements should occur first in the final sorted list.
     The only requirement is that the operator obey two of the properties
     of a total order:
             if a b and b c then a c (transitivity)
             for all a and b, either a b or b a (totalness or trichotomy).
     It is possible that both a b and b a; in this case either may come
     first in the sorted list. In a stable sort, the input order determines the
     sorted order in this case.




Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   12 / 38
Comparison Sort Disadvantages
There are fundamental limits on the performance of comparison sorts. A
comparison sort must have a lower bound of (n log n) comparison
operations. This is a consequence of the limited information available
through comparisons alone or, to put it differently, of the vague algebraic
structure of totally ordered sets. In this sense, mergesort, heapsort, and
introsort are asymptotically optimal in terms of the number of comparisons
they must perform, although this metric neglects other operations. The
non-comparison sorts above achieve O(n) performance by using operations
other than comparisons, allowing them to sidestep this lower bound
(assuming elements are constant-sized).




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   13 / 38
Comparison Sort Advantages
Comparison sort offers the notable advantage that control over the
comparison function allows sorting of many different datatypes and fine
control over how the list is sorted. For example, reversing the result of the
comparison function allows the list to be sorted in reverse; and one can
sort a list of tuples in lexicographic order by just creating a comparison
function that compares each part in sequence.




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   14 / 38
Comparison Sort Examples
Some of the most well-known comparison sorts include:
      Quick sort
      Heap sort
      Merge sort
      Intro sort
      Insertion sort
      Selection sort
      Bubble sort
      Odd-even sort
      Cocktail sort
      Cycle sort
      Merge insertion (Ford-Johnson) sort
      Smoothsort
      Timsort
 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   15 / 38
Non-Comparison Sorts
There are many integer sorting algorithms that are not comparison sorts;
they include:
      Radix sort (examines individual bits of keys)
      Counting sort (indexes using key values)
      Bucket sort (examines bits of keys)




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   16 / 38
Stability
      Stable sorting algorithms maintain the relative order of records with
      equal keys.
      A key is that portion of the record which is the basis for the sort; it
      may or may not include all of the record.
      If all keys are different then this distinction is not necessary.
      But if there are equal keys, then a sorting algorithm is stable if
      whenever there are two records (let’s say R and S) with the same key,
      and R appears before S in the original list, then R will always appear
      before S in the sorted list.
      When equal elements are indistinguishable, such as with integers, or
      more generally, any data where the entire element is the key, stability
      is not an issue.



 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   17 / 38
Stability (Cont.)
      However, assume that the following pairs of numbers are to be sorted
      by their first component:
              (4, 2) (3, 7) (3, 1) (5, 6)
              (3, 7) (3, 1) (4, 2) (5, 6) (order maintained)
              (3, 1) (3, 7) (4, 2) (5, 6) (order changed)
      Unstable sorting algorithms can be specially implemented to be stable.
      One way of doing this is to artificially extend the key comparison, so
      that comparisons between two objects with otherwise equal keys are
      decided using the order of the entries in the original data order as a
      tie-breaker.
      Remembering this order, however, often involves an additional
      computational cost.




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   18 / 38
Bubble Sort
     Simple sorting algorithm that works by repeatedly stepping through
     the list to be sorted, comparing each pair of adjacent items and
     swapping them if they are in the wrong order.
     The pass through the list is repeated until no swaps are needed,
     which indicates that the list is sorted.
     The algorithm gets its name from the way smaller elements ”bubble”
     to the top of the list. Because it only uses comparisons to operate on
     elements, it is a comparison sort.
     Although the algorithm is simple, most other algorithms are more
     efficient for sorting large lists.




Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   19 / 38
Bubble Sort (Cont.)
                                  Graphical Illustration

An example on bubble sort. Starting from the beginning of the list,
compare every adjacent pair, swap their position if they are not in the
right order (the latter one is smaller than the former one). After each
iteration, one less element (the last one) is needed to be compared until
there are no more elements left to be compared.
http://upload.wikimedia.org/wikipedia/commons/c/c8/
Bubble-sort-example-300px.gif




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   20 / 38
Bubble Sort (Step-by-step example)
Let us take the array of numbers ”5 1 4 2 8”, and sort the array from
lowest number to greatest number using bubble sort. In each step,
elements written in bold are being compared. Three passes will be
required.
     First Pass:
              ( 5 1 4 2 8 ) ( 1 5 4 2 8 ), Here, algorithm compares the first two
              elements, and swaps since 5 > 1.
              ( 1 5 4 2 8 ) ( 1 4 5 2 8 ), Swap since 5 > 4
              ( 1 4 5 2 8 ) ( 1 4 2 5 8 ), Swap since 5 > 2
              ( 1 4 2 5 8 ) ( 1 4 2 5 8 ), Now, since these elements are already in
              order (8 > 5), algorithm does not swap them.
      Second Pass:
              (   14258       )   (   1   4   2   5   8   )
              (   14258       )   (   1   2   4   5   8   ), Swap since 4 > 2
              (   12458       )   (   1   2   4   5   8   )
              (   12458       )   (   1   2   4   5   8   )


 Dr.Haitham A. El-Ghareeb (CIS)           Data Structures and Algorithms - 2012   November 11, 2012   21 / 38
Bubble Sort (Step-by-step example)
Now, the array is already sorted, but our algorithm does not know if it is
completed. The algorithm needs one whole pass without any swap to
know it is sorted.
    Third Pass:
              (   12458       )   (   1   2   4   5   8   )
              (   12458       )   (   1   2   4   5   8   )
              (   12458       )   (   1   2   4   5   8   )
              (   12458       )   (   1   2   4   5   8   )




 Dr.Haitham A. El-Ghareeb (CIS)           Data Structures and Algorithms - 2012   November 11, 2012   22 / 38
Pseudocode implementation

procedure bubbleSort ( A : list of sortable items )
   repeat
     swapped = false
     for i = 1 to length ( A ) − 1 inclusive do :
       /∗ i f this pair is out of order ∗/
       i f A [ i −1] > A [ i ] t h e n
          /∗ swap them and remember something changed ∗/
          swap ( A [ i −1] , A [ i ] )
          swapped = true
       end i f
     end for
   until not swapped
end procedure




Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   23 / 38
Bubble Sort Implementation in C#

public void BubbleSort ( ) {
int temp ;
for ( int outer = upper ; outer >= 1 ; outer −−) {
for ( int inner = 0 ; inner <= outer −1; inner++)
i f ( ( int ) arr [ inner ] > arr [ inner +1]) {
               temp = arr [ inner ] ;
               arr [ inner ] = arr [ inner + 1 ] ;
               arr [ inner +1] = temp ;
}
}
}




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   24 / 38
Bubble Sort Properties
      Stable
      O(1) extra space
      O(n2) comparisons and swaps
      Adaptive: O(n) when nearly sorted




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   25 / 38
Selection Sort
      Selection sort is a sorting algorithm, specifically an in-place
      comparison sort.
      It has O(n2) time complexity, making it inefficient on large lists, and
      generally performs worse than the similar insertion sort.
      Selection sort is noted for its simplicity, and also has performance
      advantages over more complicated algorithms in certain situations,
      particularly where auxiliary memory is limited.




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   26 / 38
Selection Sort in Steps
The algorithm works as follows:
      Find the minimum value in the list
      Swap it with the value in the first position
      Repeat the steps above for the remainder of the list (starting at the
      second position and advancing each time)
Effectively, the list is divided into two parts: the sublist of items already
sorted, which is built up from left to right and is found at the beginning,
and the sublist of items remaining to be sorted, occupying the remainder
of the array.




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   27 / 38
Selection Sort Algorithm

for i = 1 : n ,
    k = i
    for j = i +1: n ,             if a[j] < a[k] , k = j
    swap a [ i , k ]
end




 Dr.Haitham A. El-Ghareeb (CIS)       Data Structures and Algorithms - 2012   November 11, 2012   28 / 38
Graphical Illustration
http://upload.wikimedia.org/wikipedia/commons/9/94/
Selection-Sort-Animation.gif




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   29 / 38
Selection Sort Step by Step
      64 25 12 22 11
      11 25 12 22 64
      11 12 25 22 64
      11 12 22 25 64
      11 12 22 25 64




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   30 / 38
Selection Sort Implementation

public void SelectionSort ( ) {
int min , temp ;
for ( int outer = 0 ; outer <= upper ; outer++) {
        min = outer ;
        for ( int inner = outer + 1 ; inner <= upper ; inner++)
          i f ( arr [ inner ] < arr [ min ] )
              min = inner ;
        temp = arr [ outer ] ;
        arr [ outer ] = arr [ min ] ;
        arr [ min ] = temp ;
} }




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   31 / 38
Selection Sort Properties
      Not stable
      O(1) extra space
      (n2) comparisons
      (n) swaps
      Not adaptive




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   32 / 38
Insertion Sort
When humans manually sort something (for example, a deck of playing
cards), most use a method that is similar to insertion sort.




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   33 / 38
Insertion Sort in Words
The Insertion sort has two loops. The outer loop moves element by
element through the array whereas the inner loop compares the element
chosen in the outer loop to the element next to it in the array. If the
element selected by the outer loop is less than the element selected by the
inner loop, array elements are shifted over to the right to make room for
the inner loop element.




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   34 / 38
Graphical Illustration
http://upload.wikimedia.org/wikipedia/commons/0/0f/
Insertion-sort-example-300px.gif




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   35 / 38
Insertion Sort Step by Step
To Sort the sequence 3, 7, 4, 9, 5, 2, 6, 1 we need 8 steps. In each step,
the item under consideration is in Bold.
      37495261
      37495261
      37495261
      34795261
      34795261
      34579261
      23457961
      23456791
      12345679



 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   36 / 38
Insertion Sort Implementation in C#

public void InsertionSort ( ) {
int inner , temp ;
for ( int outer = 1 ; outer <= upper ; outer++) {
temp = arr [ outer ] ;
inner = outer ;
w h i l e ( inner > 0 && arr [ inner −1] >= temp ) {
               arr [ inner ] = arr [ inner − 1 ] ;
  inner −= 1 ;
}
             arr [ inner ] = temp ;
} }




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   37 / 38
Insertion Sort Properties
      Stable
      O(1) extra space
      O(n2) comparisons and swaps
      Adaptive: O(n) time when nearly sorted
      Very low overhead




 Dr.Haitham A. El-Ghareeb (CIS)   Data Structures and Algorithms - 2012   November 11, 2012   38 / 38

More Related Content

What's hot

Presentation on queue
Presentation on queuePresentation on queue
Presentation on queueRojan Pariyar
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IMohamed Loey
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structureSajid Marwat
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & DeletionAfaq Mansoor Khan
 
Functional dependancy
Functional dependancyFunctional dependancy
Functional dependancyVisakh V
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSGokul Hari
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructurerajshreemuthiah
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure shameen khan
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingR Islam
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sortDistrict Administration
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary searchZia Ush Shamszaman
 

What's hot (20)

Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
 
Double ended queue
Double ended queueDouble ended queue
Double ended queue
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
Functional dependancy
Functional dependancyFunctional dependancy
Functional dependancy
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Heaps
HeapsHeaps
Heaps
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsing
 
stack & queue
stack & queuestack & queue
stack & queue
 
Searching
SearchingSearching
Searching
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
 
Linked list
Linked listLinked list
Linked list
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
 
Insertion sort
Insertion sort Insertion sort
Insertion sort
 
Sorting
SortingSorting
Sorting
 

Viewers also liked

Viewers also liked (11)

Sorting
SortingSorting
Sorting
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysis
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
 
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
 
Sorting
SortingSorting
Sorting
 
Data Structures & Algorithm design using C
Data Structures & Algorithm design using C Data Structures & Algorithm design using C
Data Structures & Algorithm design using C
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Introduction to data structures and Algorithm
Introduction to data structures and AlgorithmIntroduction to data structures and Algorithm
Introduction to data structures and Algorithm
 

Similar to Lecture 07 Data Structures - Basic Sorting

Analysis and Comparative of Sorting Algorithms
Analysis and Comparative of Sorting AlgorithmsAnalysis and Comparative of Sorting Algorithms
Analysis and Comparative of Sorting Algorithmsijtsrd
 
Algorithms.pptx
Algorithms.pptxAlgorithms.pptx
Algorithms.pptxjohn6938
 
Data structures - Introduction
Data structures - IntroductionData structures - Introduction
Data structures - IntroductionDeepaThirumurugan
 
PROPOSAL OF A TWO WAY SORTING ALGORITHM AND PERFORMANCE COMPARISON WITH EXIST...
PROPOSAL OF A TWO WAY SORTING ALGORITHM AND PERFORMANCE COMPARISON WITH EXIST...PROPOSAL OF A TWO WAY SORTING ALGORITHM AND PERFORMANCE COMPARISON WITH EXIST...
PROPOSAL OF A TWO WAY SORTING ALGORITHM AND PERFORMANCE COMPARISON WITH EXIST...IJCSEA Journal
 
introduction about data structure_i.pptx
introduction about data structure_i.pptxintroduction about data structure_i.pptx
introduction about data structure_i.pptxpoonamsngr
 
Review on Sorting Algorithms A Comparative Study
Review on Sorting Algorithms A Comparative StudyReview on Sorting Algorithms A Comparative Study
Review on Sorting Algorithms A Comparative StudyCSCJournals
 
Skip List: Implementation, Optimization and Web Search
Skip List: Implementation, Optimization and Web SearchSkip List: Implementation, Optimization and Web Search
Skip List: Implementation, Optimization and Web SearchCSCJournals
 
Introductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueIntroductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueGhaffar Khan
 
DATA STRUCTURE AND ALGORITHMS
DATA STRUCTURE AND ALGORITHMS DATA STRUCTURE AND ALGORITHMS
DATA STRUCTURE AND ALGORITHMS Adams Sidibe
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptxsarala9
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structuresunilchute1
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structuresunilchute1
 

Similar to Lecture 07 Data Structures - Basic Sorting (20)

Sorting algorithm
Sorting algorithmSorting algorithm
Sorting algorithm
 
Algorithms Analysis
Algorithms Analysis Algorithms Analysis
Algorithms Analysis
 
Analysis and Comparative of Sorting Algorithms
Analysis and Comparative of Sorting AlgorithmsAnalysis and Comparative of Sorting Algorithms
Analysis and Comparative of Sorting Algorithms
 
Binary Sort
Binary SortBinary Sort
Binary Sort
 
Algorithms.pptx
Algorithms.pptxAlgorithms.pptx
Algorithms.pptx
 
Data structures - Introduction
Data structures - IntroductionData structures - Introduction
Data structures - Introduction
 
PROPOSAL OF A TWO WAY SORTING ALGORITHM AND PERFORMANCE COMPARISON WITH EXIST...
PROPOSAL OF A TWO WAY SORTING ALGORITHM AND PERFORMANCE COMPARISON WITH EXIST...PROPOSAL OF A TWO WAY SORTING ALGORITHM AND PERFORMANCE COMPARISON WITH EXIST...
PROPOSAL OF A TWO WAY SORTING ALGORITHM AND PERFORMANCE COMPARISON WITH EXIST...
 
introduction about data structure_i.pptx
introduction about data structure_i.pptxintroduction about data structure_i.pptx
introduction about data structure_i.pptx
 
Review on Sorting Algorithms A Comparative Study
Review on Sorting Algorithms A Comparative StudyReview on Sorting Algorithms A Comparative Study
Review on Sorting Algorithms A Comparative Study
 
Skip List: Implementation, Optimization and Web Search
Skip List: Implementation, Optimization and Web SearchSkip List: Implementation, Optimization and Web Search
Skip List: Implementation, Optimization and Web Search
 
Introductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueIntroductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, Queue
 
DATA STRUCTURE AND ALGORITHMS
DATA STRUCTURE AND ALGORITHMS DATA STRUCTURE AND ALGORITHMS
DATA STRUCTURE AND ALGORITHMS
 
Unit08 dbms
Unit08 dbmsUnit08 dbms
Unit08 dbms
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptx
 
Algorithms and Data Structures~hmftj
Algorithms and Data Structures~hmftjAlgorithms and Data Structures~hmftj
Algorithms and Data Structures~hmftj
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 
CSE 443 (1).pptx
CSE 443 (1).pptxCSE 443 (1).pptx
CSE 443 (1).pptx
 
DSA - Lecture 03
DSA - Lecture 03DSA - Lecture 03
DSA - Lecture 03
 
Ch1
Ch1Ch1
Ch1
 

More from Haitham El-Ghareeb (20)

مختصر وحدة التعلم الذاتي 2015
مختصر وحدة التعلم الذاتي 2015مختصر وحدة التعلم الذاتي 2015
مختصر وحدة التعلم الذاتي 2015
 
وحدة التعلم الذاتي 2015
وحدة التعلم الذاتي 2015وحدة التعلم الذاتي 2015
وحدة التعلم الذاتي 2015
 
NoSQL Databases, Not just a Buzzword
NoSQL Databases, Not just a Buzzword NoSQL Databases, Not just a Buzzword
NoSQL Databases, Not just a Buzzword
 
EMC Academic Alliance Presentation
EMC Academic Alliance PresentationEMC Academic Alliance Presentation
EMC Academic Alliance Presentation
 
DSA - 2012 - Conclusion
DSA - 2012 - ConclusionDSA - 2012 - Conclusion
DSA - 2012 - Conclusion
 
Lecture 9 - DSA - Python Data Structures
Lecture 9 - DSA - Python Data StructuresLecture 9 - DSA - Python Data Structures
Lecture 9 - DSA - Python Data Structures
 
Data Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study NotesData Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study Notes
 
Lect07
Lect07Lect07
Lect07
 
LectureNotes-06-DSA
LectureNotes-06-DSALectureNotes-06-DSA
LectureNotes-06-DSA
 
LectureNotes-05-DSA
LectureNotes-05-DSALectureNotes-05-DSA
LectureNotes-05-DSA
 
LectureNotes-04-DSA
LectureNotes-04-DSALectureNotes-04-DSA
LectureNotes-04-DSA
 
LectureNotes-03-DSA
LectureNotes-03-DSALectureNotes-03-DSA
LectureNotes-03-DSA
 
LectureNotes-02-DSA
LectureNotes-02-DSALectureNotes-02-DSA
LectureNotes-02-DSA
 
LectureNotes-01-DSA
LectureNotes-01-DSALectureNotes-01-DSA
LectureNotes-01-DSA
 
Lecture-05-DSA
Lecture-05-DSALecture-05-DSA
Lecture-05-DSA
 
Learn Latex
Learn LatexLearn Latex
Learn Latex
 
Research Methodologies - Lecture 02
Research Methodologies - Lecture 02Research Methodologies - Lecture 02
Research Methodologies - Lecture 02
 
DSA-Lecture-05
DSA-Lecture-05DSA-Lecture-05
DSA-Lecture-05
 
DSA - Lecture 04
DSA - Lecture 04DSA - Lecture 04
DSA - Lecture 04
 
Ph.D. Registeration seminar
Ph.D. Registeration seminarPh.D. Registeration seminar
Ph.D. Registeration seminar
 

Recently uploaded

Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 

Recently uploaded (20)

Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 

Lecture 07 Data Structures - Basic Sorting

  • 1. Basic Comparison Sort in Data Structures Dr.Haitham A. El-Ghareeb Information Systems Department Faculty of Computers and Information Sciences Mansoura University helghareeb@gmail.com November 11, 2012 Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 1 / 38
  • 2. Sorting Sorting is any process of arranging items in some sequence and/or in different sets, and accordingly, it has two common, yet distinct meanings: Ordering: arranging items of the same kind, class, nature, etc. in some ordered sequence, Categorizing: grouping and labeling items with similar properties together (by sorts). Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 2 / 38
  • 3. Order Theory Order theory is a branch of mathematics which investigates our intuitive notion of order using binary relations. It provides a formal framework for describing statements such as ”this is less than that” or ”this precedes that”. Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 3 / 38
  • 4. Sorting Information / Data Sorting n-tuples (depending on context also called e.g. records consisting of fields) can be done based on one or more of its components. More generally objects can be sorted based on a property. Such a component or property is called a sort key. For example, the items are books, the sort key is the title, subject or author, and the order is alphabetical. Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 4 / 38
  • 5. Weak Order If the sort key values are totally ordered, the sort key defines a weak order of the items: items with the same sort key are equivalent with respect to sorting. If different items have different sort key values then this defines a unique order of the items. Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 5 / 38
  • 6. Standard Order A standard order is often called ascending (corresponding to the fact that the standard order of numbers is ascending, i.e. A to Z, 0 to 9), the reverse order descending (Z to A, 9 to 0). For dates/times ascending means that earlier values precede later ones e.g. 1/1/2012 will sort ahead of 1/1/2013. Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 6 / 38
  • 7. Sorting in Computer Science Sorting is one of the most extensively researched subjects because of the need to speed up the operation on thousands or millions of records during a search operation. The main purpose of sorting information is to optimise its usefulness for specific tasks. Most common sorting purposes are Name, by Location and by Time (these are actually special cases of category and hierarchy). Together these give the acronym LATCH (Location, Alphabetical, Time, Category, Hierarchy) and can be used to describe just about every type of ordered information. Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 7 / 38
  • 8. Opposite of Sorting The opposite of sorting, rearranging a sequence of items in a random or meaningless order, is called Shuffling. Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 8 / 38
  • 9. Sorting Algorithm In computer science, 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 (generalization of Alphabetical order). Efficient sorting is important for 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. Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 9 / 38
  • 10. Sorting Algorithm (Cont.) More formally, the output must satisfy two conditions: The output is in nondecreasing order (each element is no smaller than the previous element according to the desired total order); The output is a permutation (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. Although many consider it a solved problem, useful new sorting algorithms are still being invented (for example, library sort was first published in 2006). Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 10 / 38
  • 11. Sorting Algorithms Classification Sorting algorithms used in computer science are often classified by: Computational complexity (worst, average and best behavior) of element comparisons in terms of the size of the list (n). For typical sorting algorithms good behavior is O(n log n) and bad behavior is O(n2). Ideal behavior for a sort is O(n), 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 O(n log n) 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”. Strictly, an in place sort needs only O(1) memory beyond the items being sorted; sometimes O(log(n)) additional memory is considered ”in place”. 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). Whether or not they11, 2012 Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November are a 11 / 38
  • 12. Comparison Sort A comparison sort is a type of sorting algorithm that only reads the list elements through a single abstract comparison operation (often a ”less than or equal to” operator) that determines which of two elements should occur first in the final sorted list. The only requirement is that the operator obey two of the properties of a total order: if a b and b c then a c (transitivity) for all a and b, either a b or b a (totalness or trichotomy). It is possible that both a b and b a; in this case either may come first in the sorted list. In a stable sort, the input order determines the sorted order in this case. Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 12 / 38
  • 13. Comparison Sort Disadvantages There are fundamental limits on the performance of comparison sorts. A comparison sort must have a lower bound of (n log n) comparison operations. This is a consequence of the limited information available through comparisons alone or, to put it differently, of the vague algebraic structure of totally ordered sets. In this sense, mergesort, heapsort, and introsort are asymptotically optimal in terms of the number of comparisons they must perform, although this metric neglects other operations. The non-comparison sorts above achieve O(n) performance by using operations other than comparisons, allowing them to sidestep this lower bound (assuming elements are constant-sized). Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 13 / 38
  • 14. Comparison Sort Advantages Comparison sort offers the notable advantage that control over the comparison function allows sorting of many different datatypes and fine control over how the list is sorted. For example, reversing the result of the comparison function allows the list to be sorted in reverse; and one can sort a list of tuples in lexicographic order by just creating a comparison function that compares each part in sequence. Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 14 / 38
  • 15. Comparison Sort Examples Some of the most well-known comparison sorts include: Quick sort Heap sort Merge sort Intro sort Insertion sort Selection sort Bubble sort Odd-even sort Cocktail sort Cycle sort Merge insertion (Ford-Johnson) sort Smoothsort Timsort Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 15 / 38
  • 16. Non-Comparison Sorts There are many integer sorting algorithms that are not comparison sorts; they include: Radix sort (examines individual bits of keys) Counting sort (indexes using key values) Bucket sort (examines bits of keys) Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 16 / 38
  • 17. Stability Stable sorting algorithms maintain the relative order of records with equal keys. A key is that portion of the record which is the basis for the sort; it may or may not include all of the record. If all keys are different then this distinction is not necessary. But if there are equal keys, then a sorting algorithm is stable if whenever there are two records (let’s say R and S) with the same key, and R appears before S in the original list, then R will always appear before S in the sorted list. When equal elements are indistinguishable, such as with integers, or more generally, any data where the entire element is the key, stability is not an issue. Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 17 / 38
  • 18. Stability (Cont.) However, assume that the following pairs of numbers are to be sorted by their first component: (4, 2) (3, 7) (3, 1) (5, 6) (3, 7) (3, 1) (4, 2) (5, 6) (order maintained) (3, 1) (3, 7) (4, 2) (5, 6) (order changed) Unstable sorting algorithms can be specially implemented to be stable. One way of doing this is to artificially extend the key comparison, so that comparisons between two objects with otherwise equal keys are decided using the order of the entries in the original data order as a tie-breaker. Remembering this order, however, often involves an additional computational cost. Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 18 / 38
  • 19. Bubble Sort Simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements ”bubble” to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, most other algorithms are more efficient for sorting large lists. Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 19 / 38
  • 20. Bubble Sort (Cont.) Graphical Illustration An example on bubble sort. Starting from the beginning of the list, compare every adjacent pair, swap their position if they are not in the right order (the latter one is smaller than the former one). After each iteration, one less element (the last one) is needed to be compared until there are no more elements left to be compared. http://upload.wikimedia.org/wikipedia/commons/c/c8/ Bubble-sort-example-300px.gif Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 20 / 38
  • 21. Bubble Sort (Step-by-step example) Let us take the array of numbers ”5 1 4 2 8”, and sort the array from lowest number to greatest number using bubble sort. In each step, elements written in bold are being compared. Three passes will be required. First Pass: ( 5 1 4 2 8 ) ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1. ( 1 5 4 2 8 ) ( 1 4 5 2 8 ), Swap since 5 > 4 ( 1 4 5 2 8 ) ( 1 4 2 5 8 ), Swap since 5 > 2 ( 1 4 2 5 8 ) ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them. Second Pass: ( 14258 ) ( 1 4 2 5 8 ) ( 14258 ) ( 1 2 4 5 8 ), Swap since 4 > 2 ( 12458 ) ( 1 2 4 5 8 ) ( 12458 ) ( 1 2 4 5 8 ) Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 21 / 38
  • 22. Bubble Sort (Step-by-step example) Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted. Third Pass: ( 12458 ) ( 1 2 4 5 8 ) ( 12458 ) ( 1 2 4 5 8 ) ( 12458 ) ( 1 2 4 5 8 ) ( 12458 ) ( 1 2 4 5 8 ) Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 22 / 38
  • 23. Pseudocode implementation procedure bubbleSort ( A : list of sortable items ) repeat swapped = false for i = 1 to length ( A ) − 1 inclusive do : /∗ i f this pair is out of order ∗/ i f A [ i −1] > A [ i ] t h e n /∗ swap them and remember something changed ∗/ swap ( A [ i −1] , A [ i ] ) swapped = true end i f end for until not swapped end procedure Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 23 / 38
  • 24. Bubble Sort Implementation in C# public void BubbleSort ( ) { int temp ; for ( int outer = upper ; outer >= 1 ; outer −−) { for ( int inner = 0 ; inner <= outer −1; inner++) i f ( ( int ) arr [ inner ] > arr [ inner +1]) { temp = arr [ inner ] ; arr [ inner ] = arr [ inner + 1 ] ; arr [ inner +1] = temp ; } } } Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 24 / 38
  • 25. Bubble Sort Properties Stable O(1) extra space O(n2) comparisons and swaps Adaptive: O(n) when nearly sorted Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 25 / 38
  • 26. Selection Sort Selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and also has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited. Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 26 / 38
  • 27. Selection Sort in Steps The algorithm works as follows: Find the minimum value in the list Swap it with the value in the first position Repeat the steps above for the remainder of the list (starting at the second position and advancing each time) Effectively, the list is divided into two parts: the sublist of items already sorted, which is built up from left to right and is found at the beginning, and the sublist of items remaining to be sorted, occupying the remainder of the array. Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 27 / 38
  • 28. Selection Sort Algorithm for i = 1 : n , k = i for j = i +1: n , if a[j] < a[k] , k = j swap a [ i , k ] end Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 28 / 38
  • 29. Graphical Illustration http://upload.wikimedia.org/wikipedia/commons/9/94/ Selection-Sort-Animation.gif Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 29 / 38
  • 30. Selection Sort Step by Step 64 25 12 22 11 11 25 12 22 64 11 12 25 22 64 11 12 22 25 64 11 12 22 25 64 Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 30 / 38
  • 31. Selection Sort Implementation public void SelectionSort ( ) { int min , temp ; for ( int outer = 0 ; outer <= upper ; outer++) { min = outer ; for ( int inner = outer + 1 ; inner <= upper ; inner++) i f ( arr [ inner ] < arr [ min ] ) min = inner ; temp = arr [ outer ] ; arr [ outer ] = arr [ min ] ; arr [ min ] = temp ; } } Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 31 / 38
  • 32. Selection Sort Properties Not stable O(1) extra space (n2) comparisons (n) swaps Not adaptive Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 32 / 38
  • 33. Insertion Sort When humans manually sort something (for example, a deck of playing cards), most use a method that is similar to insertion sort. Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 33 / 38
  • 34. Insertion Sort in Words The Insertion sort has two loops. The outer loop moves element by element through the array whereas the inner loop compares the element chosen in the outer loop to the element next to it in the array. If the element selected by the outer loop is less than the element selected by the inner loop, array elements are shifted over to the right to make room for the inner loop element. Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 34 / 38
  • 35. Graphical Illustration http://upload.wikimedia.org/wikipedia/commons/0/0f/ Insertion-sort-example-300px.gif Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 35 / 38
  • 36. Insertion Sort Step by Step To Sort the sequence 3, 7, 4, 9, 5, 2, 6, 1 we need 8 steps. In each step, the item under consideration is in Bold. 37495261 37495261 37495261 34795261 34795261 34579261 23457961 23456791 12345679 Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 36 / 38
  • 37. Insertion Sort Implementation in C# public void InsertionSort ( ) { int inner , temp ; for ( int outer = 1 ; outer <= upper ; outer++) { temp = arr [ outer ] ; inner = outer ; w h i l e ( inner > 0 && arr [ inner −1] >= temp ) { arr [ inner ] = arr [ inner − 1 ] ; inner −= 1 ; } arr [ inner ] = temp ; } } Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 37 / 38
  • 38. Insertion Sort Properties Stable O(1) extra space O(n2) comparisons and swaps Adaptive: O(n) time when nearly sorted Very low overhead Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 11, 2012 38 / 38