Click here to load reader

Sorting pnk

Embed Size (px)

Citation preview

Slide 1

- PINAK PATELSorting Algorithms

ComplexityMost of the primary sorting algorithms run on different space and time complexity.

Time Complexity is defined to be the time the computer takes to run a program (or algorithm in our case).

Space complexity is defined to be the amount of memory the computer needs to run a program.

Complexity in general, measures the algorithms efficiency in internal factors such as the time needed to run an algorithm.

Big O AnalysisWhen solving a computer science problem there will usually be more than just one solution.These solution often be in the form of different algorithms.Big O analysis will help to measure the efficiency of the algorithm.Big O notation is used to classify algorithms by how they respond to changes in input size in terms of time and space.Big O only provides the upper bound on the growth rate of the function.

Bubble sortExplanation: Compare each element (except last one) with its neighbour to the rightIf they are out of order, swap themThis puts the largest element at the very endThe last element is now in the correct and final phaseCompare each element (except last one) with its neighbour to the rightIf they are out of order, swap themThis puts the second largest element next to lastThe last two elements are now in their correct and final phase

Compare each element (except last one) with its neighbour to the rightContinue as above until you have no unsorted elements on the left

Bubble sortExample:

7, 2, 8, 5, 4 2, 7, 5, 4, 8 2, 5, 4, 7, 82, 7, 8, 5, 4 2, 5, 7, 4, 8 2, 4, 5, 7, 82, 7, 5, 8, 4 2, 5, 4, 7, 8 done2, 7, 5, 4, 8

AlgorithmBubble_Sort(int[] a){n=length of afor i=0 to n-1{for j=0 to n-i{ If(a[j] > a[j+1]) {temp=a[J];a[j]=a[j+1];a[j+1]=temp; }}}}

AnalysisRunning time:Worst case: O(N2)Best case: O(N)

Selection sortExplanation:

Given an array of length n;Search element 0 through n-1 and select the smallestSwap it with the element at location 0;

Search element 1 through n-1 and select the smallestSwap it with the element at location 1;Search element 2 through n-1 and select the smallestSwap it with the element at location 2;Continue until there is nothing left to search.

Selection sortExample:

7, 2, 8, 5, 42, 7, 8, 5, 42, 4, 8, 5, 72, 4, 5, 8, 72, 4, 5, 7, 8

AlgorithmselectionSort(int[] a){n=length(a)for i=0 to n-1{min=i; for j=i+1 to n{if(a[j] < a[min]){min=j;}}temp=a[outer];a[outer]=a[min];a[min]=temp;}}

Analysis

The outer loop executes n-1 timeThe inner loop executes n/2 times on average.Time required is roughly (n-1)*(n/2)Time complexity = O(n^2)

Insertion sortExplanation:

The basic idea of Insertion Sort algorithm can be described as these steps:1. Data elements are grouped into two sections: a sorted section and an un-sorted section.2. Take an element from the un-sorted section.3. Insert the element into the sorted section at the correct position based on the comparable property.4. Repeat step 2 and 3 until no more elements left in the un-sorted section.

Adding a new element to a sorted list will keep the list sorted if the element is inserted in the correct placeA single element list is sortedInserting a second element in the proper place keeps the list sortedThis is repeated until all the elements have been inserted into the sorted part of the list

Insertion sort

Algorithm:insertionSort(A){n=len(A)For i=2 to nKey=a[i];j=i-1;While(j>0 && a[j]>key){A[j+1]=a[j];j=j-1;}A[j+1]=key;}

AnalysisBest case: If A is sorted: O (n)Worst case:The outer loop is always done N 1 timesThe inner loop does the most work when the next element is smaller than all of the past elementsOn each pass, the next element is compared to all earlier elements, giving:If A is reversed sorted: O (n^2)Average case: O (n^2)

Merge SortExplanation:

Merge Sort is a complex and fast sorting algorithm that repeatedly divides an un-sorted section into two equal sub-sections, sorts them separately and merges them correctly.

The basic idea of Merge Sort algorithm can be described as these steps:

1. Divide the data elements into two sections with equal number of elements.

2. Sort the two sections separately.

3. Merge the two sorted sections into a single sorted collection.

Obviously, this is a recursive idea, where a problem is divided into smaller problems. And the division will be repeated to make the smaller problems even smaller, until they are smaller enough so that the solutions are obvious.

AlgorithmMergeSort(A,low,high){If(low