by d.kumaragurubaran adishesh pant. introduction big o notation simple algorithms complex...

35

Upload: rodney-atkins

Post on 23-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm
Page 2: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

Introduction Big O Notation Simple algorithms Complex algorithm

Page 3: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

Algorithm is a sequence of method/instructions which can be followed to performed a specific task, such as calculation and data processing.

Page 4: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

Shorthand way to say how efficient a computer algorithm is. In computer science , this rough measure is called “Big O” notation.

For example, Automobiles are divided by size into several categories: subcompacts, compacts , midsize, and so on. These categories provide a quick idea what size car you’re talking about, without needing to mention actual dimensions.

Page 5: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

“Algorithm A is twice as fast as algorithm B” – Is it meaningful?

Why not? Because the proportion can change radically as the number of items changes.

The idea in Big O notation isn’t to give actual figures for running times but to convey how the running times are affected by the number of items.

Other factors affecting the running time are speed of the microprocessor, how efficiently the compiler has generated the program code, etc. and called as invariants.

Page 6: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

Recognizing invariants can be useful in understanding the algorithm.

In certain situations they may also be helpful in debugging; you can repeatedly check that the invariant is true, and signal an error if it isn’t.

Page 7: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

Linear search O(N) Binary search O(log N) Insertion in unordered array O(1) Insertion in ordered array O(N) Deletion in unordered array O(N) Deletion in ordered array O(N)

Page 8: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

Linear Search

Proportional to N

Average linear search time is proportional to the size of the array

T = K * N / 2=> T = K * N

N is the total number of items

T is search time

Binary Search

• Proportional to log(N)

• Binary search uses the same as children’s guessing game.

• T = K * log2(N)=>T = K * log(N)

N is the total number of items

T is search time

Page 9: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

Sorting is the process of rearranging a set of items in a specific order.

Why Sorting? Sorting is essential in data processing so

that applications would be able to access them more efficiently. Example: "Order By" or "Group By".

Sorting Algorithm is an algorithm with the purpose of rearranging items in a specific order.

Page 10: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

Sorting by Exchange - Bubble sort, Quicksort

Sorting by Insertion - Insertion sort, Shellsort

Sorting by Selection - Selection sort, Heapsort

Sorting by Merging - Merge sort

Sorting by Distribution - Radix sort

Page 11: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

The following are the fundamental operations that take place during sorting:◦ Comparison of two keys ◦ Interchange of records ◦ Assignment of a record to a temporary location

Complexity of a sorting algorithm ◦ Measures the running time as a function of n, the

number of records sorted◦ Proportional to number of comparisons(It has no

big picture, can compare only two at a time)

Page 12: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

The Bubble sort, Selection sort, and Insertion sort can be classified as simple algorithms.

Two steps, executed over and over until the data is sorted:◦ Compare two items.◦ Swap two items, or copy one item.

Page 13: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

Notoriously slow, but conceptually the simplest of the sorting algorithms - good beginning for our exploration.

Here are the rules you’re following:1. Compare two players.2. If the one on the left is taller, swap them.3. Move one position right.

You continue down the line this way until you reach the right end. You have by no means finished sorting the kids, but you do know that the tallest kid is on the right.

Page 14: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

This is why it’s called the bubble sort: As the algorithm progresses, the biggest items “bubble up” to the top end of the array.

After this first pass through all the data, you’ve made N-1 comparisons and somewhere between 0 and N-1 swaps, depending on the initial.

The item at the end of the array is sorted and won’t be moved again.

Example: (5,6,3,2,4) 1st sort– (5,6,3,2,4) (5,3,6,2,4) (5,3,2,6,4) (5,3,2,4,6)

Page 15: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

public void bubbleSort(){

int out, in;for(out=nElems-1; out>1; out--)

◦ {for(in=0; in<out; in++) {

if( a[in] > a[in+1] ) swap(in, in+1); // swap them

}}

}

Page 16: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

Although in the worst case, with the initial data inversely sorted, a swap is necessary with every comparison.

The outer loop executes N times, and the inner loop executes N times for each cycle of the outer loop.

Thus both swaps and comparisons are proportional to O(n2). So this is slow.

Page 17: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

The insertion sort, for example, is preferable for small files and for almost sorted files.

It still executes in O(n2) time, but it’s about twice as fast as the bubble sortand somewhat faster than the selection sort in normal situations.

It’s often used as the final stage of more sophisticated sorts, such as quicksort.

Page 18: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

Here a partially sorted data is considered(3,5,6,4,7).

Partially sorted – (3,5,6)Unsorted – (4,7)

The first data in the unsorted list ‘4’ is removed and stored in a temp, then compared with the sorted list from right in descending order(3,5,6).

If the data in sorted list is larger move it to the position of 4. Thus larger data are moved right one by one till a value less than ‘4’ is reached. When data being compared is small then our temp data is inserted to the right of it.

1st step - (3,5, ,6,7)2nd step – (3, ,5,6,7) 3rd step – (3,4,5,6,7)

Page 19: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

public void insertionSort(){int in, out;for(out=1; out<nElems; out++) // out is dividing line{long temp = a[out]; // remove marked itemin = out; // start shifts at outwhile(in>0 && a[in-1] >= temp) // until one is smaller,{a[in] = a[in-1]; // shift item right,

--in; // go left one position}a[in] = temp; // insert marked item} // end for} // end insertionSort()

Page 20: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

•Is an o(n^2) sorting algorithm•A simple algorithm

Page 21: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

There are two lists input list and output list1. Find the minimum value in input list, place

it in output list.2. Replace that value in input list with ∞.3. Repeat step 1 until all the values in input

list are replaced with ∞.

Page 22: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

Two lists can be avoided by doing the operation on input list.

This is achieved by avoiding placing ∞, instead the minimum value is swapped with the first element of the list.

The next search for the minimum value begins with second element, where the second element is replaced with minimum value.

The next search starts with third element and so on…

Page 23: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

Tournament picks the best player, but the runner will not always be the second best.

Page 24: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

Negative weights: replacing the strongest player with the weakest one, gives us the actual second best player.

Page 25: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

After 7 steps

Page 26: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

A simple but inefficient algorithm Faster than bubblesort but slower than

insertion sort. All the elements should be available before

sorting can begin.

Page 27: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

•Merge sort is an O(n log n) sorting algorithm.•Used for sequential sorting like tape drives.•Divide and Conquer approach

Page 28: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

One of the atomic parts of merge sort algorithm is merging sorted lists.

Page 29: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

In python

Page 30: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

Java:

Page 31: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

Extending merge sort for unsorted lists. Identifying semi-sorted lists from unsorted

lists. 1 9 12 2 6 7

1 9 12 2 6 7

Page 32: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

An unsorted list can always be divided into sorted sublists from the fact that a single element is always sorted.

9 6 10 12 65 4

9 6 10 12 65 4

9 6

Page 33: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

When unsorted list is divided into sorted sublists, apply mergeSortedlists algorithm to merge two sorted sublists to get a single sorted list.

Continue the above step to merge all the sorted sublists into one single list.

Page 34: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

There many types of merge sorts, k-way merge sorts etc.

Faster than insertion sort and bubble sort

Page 35: By D.Kumaragurubaran Adishesh Pant.  Introduction  Big O Notation  Simple algorithms  Complex algorithm

http://www.sorting-algorithms.com/