sorting n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Sorting PowerPoint Presentation
Download Presentation
Sorting

Loading in 2 Seconds...

play fullscreen
1 / 22

Sorting - PowerPoint PPT Presentation


  • 125 Views
  • Uploaded on

Sorting . Chapter 9. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is to choose the criteria that will be used to order data.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Sorting' - carlotta


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
sorting

Sorting

Chapter 9

slide2

The efficiency of data handling can often be increased if the data are sorted according to some criteria of order.

  • The first step is to choose the criteria that will be used to order data.
  • The final ordering of data can be obtained in a variety of ways\methods, and only some of them can be considered meaningful and efficient.
  • Certain critical properties of sorting algorithms should be defined when comparing alternative methods.
    • Which are the number of comparisons and the number of data movements .
9 1 elementary sorting algorithms
9.1 Elementary Sorting Algorithms
  • Insertion Sort
  • Start with considering the two first elements of the array ( ex. data[] array). If they are out of order, an interchange takes place. Then the third element is considered and inserted into its proper place and so on…

insertionSort(data[],n))

for i =1 to n-1

move all elements data[j] greater than data[i] by 1 position

Place data[i] in its proper position

slide5

To find the number of movements and comparisons performed by insertion sort, observe that:

  • The outer for loop always perform n-1 iterations.
  • The number of elements greater than data[i] to be moved by one position is not always the same.
  • The best case is when data are already in order - O(n).
  • The worst case is when data are in reverse order - O(n2 )
slide6

An insertion sort advantage is :

    • It sorts the array only when it is really necessary (ex. The array is already in order).
  • Disadvantages :
    • Overlook the fact that elements may already be in their proper positions .
    • If an item is being inserted, all elements greater than that item have to be moved.
slide8

Selection Sort

  • Finding a misplaced element first and putting it in its final place.
    • The element with the lowest value is selected and exchanged with the element in the first position.
    • The smallest value among the remaining elements is found and replaced with the second position. And so on..

selectionSort(data[],n))

for i=0 to n-2

Select the smallest elements among data[i] ….. data[n-1]

Swap it with data[i]

slide9

Example

O(n2)

slide11

Bubble Sort

  • The array is scanned from the bottom up, and two adjacent elements are interchanged if they are out of order.
    • First, items data[ n-1 ] and data[ n-2 ] are compared and swapped if they are out of order.
    • Next, data[ n-2 ] and data[ n-3 ] are compared and swapped if they are out of order and so on up to data[1] and data[0].
      • So, the smallest element is bubbled up to the top of the array..
slide12

Bubble Sort

    • The array now is scanned again comparing consecutive items and interchanging them when needed.
      • Compare up to data[2] and data[1]. So, the next smallest element is in position 1.
    • And so on until the last pass when only one comparison, data[n-1] with data[n-2], that may need to be interchanged.

selectionSort(data[],n)

for i =0 to n-2

for j=n-1 down to i+1

Swap elements in position j and j-1 if they are out of order

slide13

The main disadvantage is that it looks at two adjacent array elements at a time and swaps them if they are not in order.

    • If an element has to be moved from the bottom to the top, it is exchanged with every element in the array.
  • Also, this algorithm concentrates only on the item that is being bubbled up.
9 3 efficient sorting algorithm
9.3 Efficient Sorting Algorithm
  • Quick Sort
  • Quick sort selects one of the entries in the sequence to be the pivot and divides the sequence into two subsequences - one with all elements less than or equal to pivot are placed before it and one with all elements greater than pivot are placed after it.
  • It is one of the most common sorting algorithms for sequential computers because of its simplicity, low overhead, and optimal average complexity.
slide16

The process of sorting is recursively applied to each of the sub lists.

  • Quicksort operates in O(N*logN) time.
  • Quicksort consists of two phases:
    • Sort phase.
    • Partition phase.

QuickSort(array[])

if length(array)>1 then choose Bound

while there are elements left in the array

include element in Subarray1 or Subarray 2

QuickSort(Subarray1 )

QuickSort(Subarray2)

slide17

Algorithm steps :

    • Choose a pivot value. 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.
    • 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 to non-equal parts.
    • Sort both parts. Apply quicksort algorithm recursively to the left and the right parts of the array.
slide19

Merge Sort

  • This is a much more efficient sorting technique than the bubble Sort and the insertion Sort at least in terms of speed.
  • Although the bubble and insertion sorts take O(N2) time, the merge sort is O(N*logN).
  • Merging two sorted arrays
  • Merging two sorted arrays A and B (they don’t need to be the same size)creates a third array, C, that contains all the elements of A and B, also arranged in sorted order.
slide21

Sorting by Merging

  • The idea in the Merge sort is to divide an array in half, sort each half, and then use the merge() function to merge the two halves into a single sorted array.
  • You divide the array again and again until you reach a sub array with only one element. This is the base case; it’s assumed an array with one element is already sorted.
  • Algorithm steps :
    • Divides the array near its midpoint.
    • Sorts the two half-arrays by recursive calls.
    • Merges the two sorted half-arrays to obtain a fully sorted array.