Program: BTech (BAO/CSF/CTIS/IoT/AIML/CSE) Class: SE Course: Data Structures · 2020. 8. 18. ·...

69
Program: BTech (BAO/CSF/CTIS/IoT/AIML/CSE) Class: SE Course: Data Structures Unit- 1 : Introduction to Data Structures . Prepared By: Mr. Vipin K. Wani Assistant Professor School of Computer Sciences & Engineering

Transcript of Program: BTech (BAO/CSF/CTIS/IoT/AIML/CSE) Class: SE Course: Data Structures · 2020. 8. 18. ·...

  • Prepared By: Mr. Vipin K. Wani SOCSE, SandipUniversity, Nashik

    Program: BTech (BAO/CSF/CTIS/IoT/AIML/CSE)

    Class: SE

    Course: Data Structures

    Unit- 1 : Introduction to Data Structures

    .

    Prepared By:

    Mr. Vipin K. WaniAssistant Professor

    School of Computer Sciences & Engineering

  • Data Structures concepts

    2Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik

    1. Data:

    Data is a collection of information.

    But it contains unprocessed information.

    Processed and useful data is called as information.

    Example: any word file, text file, video file, audio file, or any type of data.

    In programming languages a data can be considered of following types

    Int, char, flot, double, long, etc

  • Data Structures concepts

    3Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik

    2. Data Objects:

    A data object is a region of storage that contains a value or group of values.

    A data object is a container which can store one or more elements of data.

    Example: array

    Image Source: [R]

    Array

    5 10 7 53 350 1 2 3 4

  • Data Structures concepts

    4Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik

    3. Data Structures: a data structure is a data organization, management, and storage format that enables efficient access

    and modification.

    More precisely, is a collection of data values, the relationships among them, and the functions or

    operations that can be applied to the data.

    Ex. Array, Trees, Graphs etc

    5 10 7 53 350 1 2 3 4

    1

    2 3

    4 5

    [A][B]

    [C]

  • Data Structures concepts

    5Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik

    4. Abstract Data Types (ADT): a ADT is a data type which is not provided by the programming language inbuilt and created by the

    user for his programming requirements.

    An abstract data type is defined by its behavior from the point of view of a user, of the data,

    specifically in terms of possible values, possible operations on data. Ex. Array, Trees, Graphs, Linked

    list etc1

    2 3

    4 5

    [A][B]

    5 7 9

  • Types of Data Structures

    6Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    1. Primitive & Non Primitives: Primitives Data Structures: a basic data type provided by a programming language as a basic

    building block. These are provided inbuilt by programming languages.

    Examples: int, Char, Float, double, etc.

    Non Primitives Data Structures: The data type that are derived from primary data types are known

    as non-primitive data type. These are created by user for his requirement.

    Examples: . Ex. Array, Trees, Graphs, Linked list etc

  • Types of Data Structures

    7Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    2. Linear & Non Linear : Linear Data Structures: is a data structure in which data items are stored linearly in the memory. So

    there is contiguous memory allocation of the data.

    Examples: int, Arrays, Linked List, Stack, Queue etc.

    Non Linear Data Structures: is a data structure in which data items are not stored linearly in the

    memory. So there is no contiguous memory allocation of the data.

    Examples: . Ex. Trees, Graphs. 1

    2 3

    4 5

    5 10 7 53 350 1 2 3 4

  • Types of Data Structures

    8Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    3. Static & Dynamic Data Structures: Static Data Structures: In Static data structure the size of the structure is fixed. The content of the

    data structure can be modified but without changing the memory space allocated to it.

    Examples: Arrays, Linked List, Stack, Queue etc.

    Dynamic Data Structures: In Dynamic data structure the size of the structure in not fixed and can be

    modified during the operations performed on it.

    Memory allocated at run time.

    Examples: . Ex. Trees, Graphs.

    1

    2 3

    4 5

    5 10 7 53 350 1 2 3 4

  • Types of Data Structures

    9Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    4. Persistent & Ephemeral Data Structures: Persistent Data Structures: is a data structure that always preserves the previous

    version of itself when it is modified. Such data structures are immutable.

    Examples: Stack.

    102010

    302010

    2010

    Push 10 Push 20 Push 30 Pop 30

    10

  • Types of Data Structures

    10Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    4. Persistent & Ephemeral Data Structures:

    Ephemeral Data Structures: is a data structure that does not preserves the previous version of

    itself when it is modified. Previous states of data types can not be retained in this data types.

    Examples: Queue.

    10

    10

    10

    10 20

    10 20 30

    20 30

    Insert 10

    Insert 20

    Insert 30

    Remove 10

  • Introduction to algorithms

    11Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    1. Algorithms: an algorithm is a finite sequence of well-defined, computer-implementable instructions, typically to solve a

    class of problems or to perform a computation.

    Is a step by step solution of a problem.

    Examples: Algorithm to compute addition of two numbers.

    1. Start.2. Declare three numbers a, b & c3. Accept Number one a.4. Accept Number two b.5. Add both the numbers and store result in c .6. Print the result c.7. End

  • Introduction to algorithms

    12Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    2. Algorithms Design Tools: a. Flow Charts: A flowchart is a type of diagram that represents a workflow or process.

    A flowchart can also be defined as a diagrammatic representation of an algorithm, a

    step-by-step approach to solving a task. The flowchart shows the steps as boxes of

    various kinds, and their order by connecting the boxes with arrows.

  • Introduction to algorithms

    13Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Sr. No. Component Name Component Symbol Purpose

    1 The Oval To Indicate An End or a Beginning

    2 The Rectangle A Step in the Flowcharting Process

    3 The Arrow Indicate Directional Flow

    4 The DiamondIndicate a Decision

    5 Input & Output Symbolsdata is coming in and out throughout your process

    Flow Charts components :

  • Introduction to algorithms

    14Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Sr. No. Component Name Component Symbol Purpose

    6 Document Symbols Document icons show that there are additional points of reference involved in your flowchart.

    7 Connecting Symbols connect flowcharts that span multiple pages.

    8 Data Symbols clarify where the data your flowchart references is being stored.

    9 Predefined Process Define predefined flow

    Flow Charts components :

  • Introduction to algorithms

    15Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Lamp Doesn't Work?

    Replace Bulb.

    Plug in Lamp.

    Repair Lamp.

    Is Lamp Plugged

    in?

    Is Bulb burned

    out?

    Yes

    Yes

    No

    No

    Flow Charts Example:

  • Introduction to algorithms

    16Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    b. Pseudo Code: Is a informal way of writing a Programme. It is a Algorithm written with

    combination of English statements and some programming statements. It is a first step to

    write a code for the algorithm.

  • Analysis of algorithms

    17Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    1. Time Complexity: the time complexity is the computational complexity that describes

    the amount of time it takes to run an algorithm. It can be computed by measuring the

    amount of time taken by basic operation of an algorithm.

    T(n)= O (b(n))

    Where :

    T(n) : Is time complexity of Algorithm.

    O : is Big Oh notation used to represent time complexity.

    b(n): is Basic operation of algorithm with input size n.

  • Analysis of algorithms

    18Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Example: Consider Linear Search example.

    for (c = 0; c < n; c++)

    {

    if (array[c] == search)

    {

    printf("%d is present at location %d.\n", search, c+1);

    break;

    }

    }

    The Basic operation may execute N time to search a key element so the complexity of algorithm is

    O(n).

    Basic Operation of algorithm

  • Analysis of algorithms

    19Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    2. Space complexity: of an algorithm quantifies the amount of space or memory taken by

    an algorithm to run as a function of the length of the input. It can be measured by

    Space Complexity = Auxiliary Space + Input space

    a) Auxiliary Space: is a temporary space occupied by the program which includesi. Variables (This include the constant values, temporary values)ii. Program Instructioniii. Execution

    b) Input space: which includes i. Space required for loopsii. File size.

  • Analysis of algorithms

    20Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    3. Frequency count: A frequency count is a measure of the number of times the each instructions

    in Programme executes. It can be calculated by summing execution count of all the instruction in a

    Programme.

    Consider a following program block.

    Int SumElements(int a[10], int n){

    int i, sum=0;For (i=0; i

  • Characteristics of good algorithms

    21Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    There can be many algorithms for a particular problem. So, how to classify an algorithm tobe good and others to be bad?

    Correctness: An algorithm is said to be correct if for every set of input it halts with thecorrect output. If you are not getting the correct output for any particular set of input,then your algorithm is wrong.

    Finiteness: The algorithm must always terminate after a finite number of steps.

    Efficiency: An efficient algorithm is always used. By the term efficiency, we mean to saythat:

    i. The computational time should be as less as possible.ii. The memory used by the algorithm should also be as less as possible.

    Unambiguity: Instructions written in a algorithms must be unambiguous

  • Asymptotic Notations

    22Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Asymptotic notations are mathematical tools to represent time complexity of

    algorithms for asymptotic analysis.

    The main idea of asymptotic analysis is to have a measure of efficiency of algorithms

    that doesn’t depend on machine specific constants, and doesn’t require algorithms to

    be executed and time taken by programs to be compared.

    The following 3 asymptotic notations are mostly used to represent time complexity

    of algorithms.

    1. Big Oh Notation (O):

    2. Omega Notation (Ω):

    3. Theta Notation (Θ):

  • Asymptotic Notations

    23Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    1. Big Oh Notation (O): The Big O notation defines an upper bound of an algorithm, it

    indicates highest possible (worst) value of time complexity .

    Consider a input function f(n) belongs to the set O(g(n)) if there exists a positive

    constant c such that it lies between 0 and g(n), for sufficiently large n i.e. 0 < cg(n) <

    n. Then

    f(n)

  • Asymptotic Notations

    24Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    2. Omega Notation (Ω): The Omega notation defines an lower bound of an algorithm,

    it indicates lowest possible (best) value of time complexity .

    if there exists a positive constant c such that it lies above cg(n), for sufficiently large n

    i.e. cg(n)C g(n)

    Then we can say

    f(n)=Ω g(n)

    It can be represented on graph as.

  • Asymptotic Notations

    25Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    3. Theta Notation (Θ): Theta notation encloses the function from above and below. Since it

    represents the upper and the lower bound of the running time of an algorithm, it is used for

    analyzing the average case complexity of an algorithm.

    if there exist positive constants c1 and c2 such that it can be sandwiched

    between c1g(n) and c2g(n), for sufficiently large n. If a function f(n) lies anywhere in

    between c1g(n) and c2 > g(n) for all n ≥ n0, then f(n) is said to be asymptotically tight bound.

    C1 g(n) < f(n)

  • Best Case, Worst Case & Average Case Analysis

    26Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    The Best Case, Worst Case & Average Case analysis is to have a measure of efficiency of algorithms

    Best Case Analysis :In the best case analysis, we calculate lower bound on running time of an algorithm. We mustknow the case that causes minimum number of operations to be executed. In the linear searchproblem, the best case occurs when x is present at the first location. So time complexity in thebest case would be Ω(1)

    Worst Case Analysis :In the worst case analysis, we calculate upper bound on running time of an algorithm. We mustknow the case that causes maximum number of operations to be executed. For Linear Search, theworst case happens when the element to be searched is not present in the array.

    Average Case Analysis:In average case analysis, we take all possible inputs and calculate computing time for all of theinputs. Sum all the calculated values and divide the sum by total number of inputs. We must know(or predict) distribution of cases.

  • Best Case, Worst Case & Average Case Analysis

    27Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Best Case Worst Case Average Case

  • Searching Techniques

    28Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Searching Techniques : Searching is the process of finding a given value position in

    a list of values. It decides whether a search key is present in the data or not. It is the

    algorithmic process of finding a particular item in a collection of items.

    Different Searching techniques are,

    1. Linear Search.

    2. Binary Search.

    3. Fibonacci Search.

  • Searching Techniques

    29Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    1. Linear Search: a linear search or sequential search is a method for finding an

    element within a list. It sequentially checks each element of the list until a match is

    found or the whole list has been searched. Time Complexity is O(n).

  • Searching Techniques

    30Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Linear Search Algorithm:

    Linear Search ( Array A, Value x)

    Step 1: Set i to

    Step 2: if i > n then go to step 7

    Step 3: if A[i] = x then go to step 6

    Step 4: Set i to i + 1

    Step 5: Go to Step 2

    Step 6: Print Element x Found at index i and go to step 8

    Step 7: Print element not found

    Step 8: Exit

  • Searching Techniques

    31Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Linear Search Pseudo code:

    procedure linear_search (list, value)

    for each item in the list

    if match item == value

    return the item's location

    end if

    end for

    end procedure

  • Searching Techniques

    32Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Divide and Conquer: Is a algorithm design strategy in which we can break a single bigproblem into smaller sub-problems, and solve the smaller sub-problems and combine theirsolutions to find the solution for the original big problem, it becomes easier to solve the wholeproblem. The concept of Divide and Conquer involves three steps:

    1. Divide the problem into multiple smallproblems.

    2. Conquer the sub problems by solving them.The idea is to break down the problem intoatomic sub problems, where they are actuallysolved.

    3. Combine the solutions of the sub problems tofind the solution of the actual problem

  • Searching Techniques

    33Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    2. Binary Search: Search a sorted array by repeatedly dividing the search interval in

    half. Begin with an interval covering the whole array. If the value of the search key is

    less than the item in the middle of the interval, narrow the interval to the lower half.

    Otherwise narrow it to the upper half. Repeatedly check until the value is found or the

    interval is empty.5 8 12 15 18 21 25 28 302

    0 1 2 3 4 5 6 7 8 9Search 18

    5 8 12 15 18 21 25 28 302L=0 1 2 3 M=4 5 6 7 8 H=9

    Check is 18 > array[M]? Yes

    Check is 18= array[M]? No.

    5 8 12 15 18 21 25 28 3020 1 2 3 4 L=5 6 M=7 8 H=9

    Check is 18= array[M]? No Check is 18 < array[M]?Yes

    5 8 12 15 18 21 25 28 3020 1 2 3 4 L=M=5 H=6 7 8 9

    Check is 18= array[M]? Yes

  • Searching Techniques

    34Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Binary Search Pseudo code :numbers to Procedure binary_search

    A ← sorted array

    n ← size of array

    x ← value to be searched

    Set lowerBound = 0

    Set upperBound = n-1

    while x not found

    if upperBound < lowerBound

    EXIT: x does not exists.

    set midPoint = (lowerBound + upperBound) / 2

  • Searching Techniques

    35Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Binary Search Pseudo code :

    If A[midPoint] < x

    set lowerBound = midPoint + 1

    if A[midPoint] > x

    set upperBound = midPoint - 1

    if A[midPoint] = x

    EXIT: x found at location midPoint

    end while

    end procedure

  • Searching Techniques

    36Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    3. Fibonacci Search.: Fibonacci Search is a comparison-based technique that uses Fibonacci numbers

    to search an element in a sorted array. Time Complexity is O(logn).

    Differences with Binary Search:

    Fibonacci Search divides given array in unequal parts

    Fibonacci Search doesn’t use /, but uses + and -. The division operator may be costly on some CPUs.

    Fibonacci Search examines relatively closer elements in subsequent steps. So when input array is big

    that cannot fit in CPU cache or even in RAM, Fibonacci Search can be useful.Example: Consider the array elements. 100, 90, 150, 30, 15, 60, 120, 10.

    The element do be found be 120

    According to the algorithm we will first sort the array.

    100 90 30 15 60150 120 10

  • Searching Techniques

    37Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Steps to Search element using Fibonacci search.

    Compute three variables a, b, f Here Initially f=n-1.

    Construct Fibonacci series to determine a & b.

    Fibonacci Series FB=x1, x2, x3, x4, x5…………..x5

    So a=x1 & b= x2

    With this initial values start searching the element, and check the following two conditions.

    if(Keyarr[f])

    f=f+a;

    b=b-a;

    a=a-b;

    1. 2.

  • Searching Techniques

    38Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Fibonacci Search Example:

    The first few Fibonacci numbers are:0,1,1,2,3,5,8,13....

    1. Initialy f=n-1=7

    2. barr[f])

    if(Key90

    So f=f+a;

    f=4+2=6

    b=b-a=3-2=1

    a=a-b=2-3=-1;

    Step 3:

    if(Key==arr[f])

    So Element Found

  • Introduction to Sorting

    39Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    What is Sorting: Sorting is nothing but arranging the data in ascending or descending order.

    Sorting arranges data in a sequence which makes searching easier.

    Orders of Sorting:

    1. Increasing Order: A sequence of values is said to be in increasing order, if the successive

    element is greater than the previous one.

    For example, 1, 3, 4, 6, 8, 9

    2. Decreasing Order: A sequence of values is said to be in decreasing order, if the successive

    element is less than the current one.

    For example, 9, 8, 6, 4, 3, 1

  • Introduction to Sorting

    40Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Stable and Not Stable Sorting1. Stable Sorting: If a sorting algorithm, after sorting the contents, does not change the sequence of

    similar content in which they appear, it is called stable sorting.

    2. Unstable Sorting: If a sorting algorithm, after sorting the contents, changes the sequence of similar content in which they appear, it is called unstable sorting.

    Stability of an algorithm matters when we wish to maintain the sequence of original elements,like in a tuple for example.

    35 70254520 56 25 40

    20 70564525 25 35 40

    35 70254520 56 25 40

    20 70564525 25 35 40

  • Introduction to Sorting

    41Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Sorting Efficiency: Sorting Efficiency is the mechanism to analyse the performance of sorting

    algorithm. There are two important factors to measure the performance which are,

    1. Time taken to sort the given data.

    2. Memory Space required to do so.

  • Sorting Techniques

    42Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    There are many different techniques available for sorting, differentiated by their efficiency and spacerequirements. Following are some sorting techniques which we will be covering in next few Lectures.

    1. Bubble Sort2. Insertion Sort3. Selection Sort4. Quick Sort5. Merge Sort

  • Sorting Techniques

    43Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Bubble Sort: This sorting algorithm is comparison-based algorithm in which each pair of adjacent

    elements is compared and the elements are swapped if they are not in order. This algorithm is not

    suitable for large data sets as its complexity is of Ο(n2) where n is the number of items.

    Ex. Let us Sort the given elements 40, 10, 20, 30, 50 Using Bubble sort.40 10 20 30 50

    40 10 20 30 50

    10 40 20 30 50

    10 20 40 30 50

    10 20 30 40 50

    Compare 40 & 10 as 40>10 Swap both elements

    Compare 40 & 20 as 40>20 Swap both elements

    Compare 40 & 30 as 40>30 Swap both elements

    Compare 40 & 50 as not 40>50 skip it.

    Pass-1

    10 20 30 40 50

    Sorted List of elements.

  • Sorting Techniques

    44Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Bubble Sort Algorithm:

    1. Start2. Declare all required variables,3. Declare array of size n.4. Read elements in array for size n.5. For each pair of elements (jth & (J+1)th elements) in array compare adjusting elements and swap

    them if the second (J+1)th element is smaller than first (jth ) element.6. Repeat the step till all elements get sorted.7. Print the sorted list of elements.8. Stop.

  • Sorting Techniques

    45Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Bubble Sort Pseudo code:

    begin BubbleSort(list)

    for all elements of list

    if list[i] > list[i+1]

    swap(list[i], list[i+1])

    end if

    end for

    return list

    end BubbleSort

  • Sorting Techniques

    46Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    insertion sort : This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained

    which is always sorted.

    For example, the lower part of an array is maintained to be sorted.

    An element which is to be inserted in this sorted sub-list, has to find its appropriate place and then it

    has to be inserted there. Hence the name, insertion sort.

    The time complexity for insertion sort is O(n2).

    Ex. Let us Sort the given elements 14, 33, 27, 10, 35, 19, 42, 44 Using insertion sort.

    14 33 27 10 35 19 42 44

    Sorted List Unsorted List

  • Sorting Techniques

    47Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    14 33 27 10 35 19 42 44

    14 33 27 10 35 19 42 44

    14 27 33 10 35 19 42 44

    10 14 27 33 35 19 42 44

    10 14 27 33 35 19 42 44

    10 14 19 27 33 35 42 44

    Compare 33 with each element of sorted list to find its place.

    Compare 27 with each element of sorted list to find its place.

    Compare 10 with each element of sorted list to find its place.

    Compare 35 with each element of sorted list to find its place.

    Compare 19 with each element of sorted list to find its place.

    Compare 42 with each element of sorted list to find its place.

  • Sorting Techniques

    48Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Compare 42 with each element of sorted list to find its place.

    Compare 44 with each element of sorted list to find its place.

    Sorted List

    10 14 19 27 33 35 42 44

    10 14 19 27 33 35 42 44

    10 14 19 27 33 35 42 44

  • Sorting Techniques

    49Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    insertion sort Algorithm:

    1. Start

    2. Declare all required variables,

    3. Declare array of size n.

    4. Read elements in array for size n.

    5. For each element of unsorted list Compare with all elements in the sorted sub-list, Shift all the

    elements in the sorted sub-list that is greater than the value to be sorted and place the element

    from unsorted list to its proper position in sorted list.

    6. Repeat the step-5 till all elements get sorted.

    7. Print the sorted list of elements.

    8. Stop.

  • Sorting Techniques

    50Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    insertion sort Pseudo code :

    INSERTION-SORT(A)

    for i = 1 to n

    key ← A [i]

    j ← i – 1

    while j > = 0 and A[j] > key

    A[j+1] ← A[j]

    j ← j – 1

    End while

    A[j+1] ← key

    End for

    14 33 27 10 35 19 42 44

  • Sorting Techniques

    51Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Selection sort : Selection sort is a simple sorting algorithm which finds the smallest element in the array

    and exchanges it with the element in the first position. Then finds the second smallest element and

    exchanges it with the element in the second position and continues until the entire array is sorted.

    The time complexity for insertion sort is O(n2). Ex. Let us Sort the given elements 14, 33, 27, 10, 35, 19, 44, 42 Using Selection sort.

    14 33 27 10 35 19 44 42Part of unsorted arrayPart of sorted arrayLeftmost element in unsorted list

    Minimum element in unsorted array

  • Sorting Techniques

    52Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    10 14 27 33 35 19 44 42

    Assumedmin

    Actualmin

    10 33 27 14 35 19 44 42

    Actualmin

    Assumedmin

    14 33 27 10 35 19 44 42

    Assumedmin

    Actualmin

    10 14 19 33 35 27 44 42

    Assumedmin

    Actualmin

    Leftmost element of unsorted part = A[0]Minimum element of unsorted part = A[3]We will swap A[0] and A[3] then, make A[0] part of sorted sub array.

    Leftmost element of unsorted part = A[1]Minimum element of unsorted part = A[3]We will swap A[1] and A[3] then, make A[1] part of sorted sub array.

    Leftmost element of unsorted part = A[2]Minimum element of unsorted part = A[5]We will swap A[2] and A[5] then, make A[2] part of sorted sub array.

    Leftmost element of unsorted part = A[3]Minimum element of unsorted part = A[5]We will swap A[3] and A[5] then, make A[3] part of sorted sub array.

  • Sorting Techniques

    53Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    10 14 19 27 35 33 44 42

    Assumedmin

    Actualmin

    10 14 19 27 33 35 44 42

    Actual /Assumedmin

    10 14 19 27 33 35 44 42

    Actual min

    Assumedmin

    10 14 19 27 33 35 44 42

    Actual /Assumed min

    10 14 19 27 33 35 42 44

    Leftmost element of unsorted part = A[4]Minimum element of unsorted part = A[5]We will swap A[4] and A[5] then, make A[4] part of sorted sub array.

    Leftmost element of unsorted part = A[5]Minimum element of unsorted part = A[5]We will swap A[5] and A[5] then, make A[5] part of sorted sub array.

    Leftmost element of unsorted part = A[6]Minimum element of unsorted part = A[7]We will swap A[6] and A[7] then, make A[6] part of sorted sub array.

    Leftmost element of unsorted part = A[7]Minimum element of unsorted part = A[7]We will swap A[7] and A[7] then, make A[7] part of sorted sub array.

    Sorted List

  • Sorting Techniques

    54Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Quick Sort: Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of

    data into smaller arrays. A large array is partitioned into two arrays one of which holds values smaller

    than the specified value, say pivot, based on which the partition is made and another array holds values

    greater than the pivot value.

    It is implemented using divide & conquer strategy.

    The time complexity for Quick sort is O(nlogn) for best case and for worst case it is O(n2).

    Ex. Let us Sort the given elements 14, 30, 27, 10, 33, 19, 44, 42 Using Quick Sort.

    14 30 27 10 33 19 42 44

  • Sorting Techniques

    55Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Quick Sort:

    Condition 1:While a[i]p;J- -;

    Condition 3:Swap a[i] & a[j];i++, j--;

    Condition 4:If I > j;Swap a[j] & pivot (p);

    14 30 27 10 33 19 42 44

    i jp

    14 30 27 10 33 19 42 44

    i jp

    14 30 27 10 33 19 42 44

    i jp14 30 27 10 33 19 42 44

    i jp

    14 30 27 10 33 19 42 44

    i jp

    A[i] < P 14 < 33 so i++

    A[i] < P 30 < 33 so i++

    A[i] < P 27 < 33 so i++

    A[i] < P 10 < 33 so i++

    A[i] < P 33 < = 33 so i++

  • Sorting Techniques

    56Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Quick Sort: A[i] < P 19 < 33 so i++

    A[i] < P 42 < 33 No

    A[j] > P 44 > 33 J--

    Now as i > j so apply 4th Condition

    Here we got two sub list now sorteach of them individually

    Now Chceck 2nd condition

    A[j] > P 42 > 33 J--

    14 30 27 10 33 19 42 44

    i jp14 30 27 10 33 19 42 44

    i jp14 30 27 10 33 19 42 44

    i jp14 30 27 10 33 19 42 44

    ijp

    14 30 27 10 19 33 42 44

    p

    Sub List 1 Sub List 2

  • Sorting Techniques

    57Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Quick Sort:

    i j

    14 30 27 10 19 33 42 44

    pi jp

    i j

    14 30 27 10 19 33 42 44

    pi jp

    i j

    14 30 27 10 19 33 42 44

    pi j p

    i j

    14 10 27 30 19 33 42 44

    pi j p

    For Sub list 1A[i] < P

    14 < 19 so i++

    A[i] < P 30 < 19No

    Now as both conditions are false apply 3rd condition

    Now check 1stcondition if itsfalse check 2nd

    condition

    Now Check 2nd condition

    A[j] > P 30 > 19 J--

    A[i]

    P i.e. 19 > 27

    so j--

  • Sorting Techniques

    58Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Quick Sort: Now as i> j so apply 4th Condition and

    Swap a[j] & pivot (p);

    Here we got two more sub list now sorteach of them individually

    Now for 3rdsub list

    A[i]< =P i.e. 14

  • Sorting Techniques

    59Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Quick Sort: 14 10 19 30 27 33 42 44

    ijp i jp

    10 14 19 30 27 33 42 44

    i jp

    10 14 19 30 27 33 42 44

    i jp

    10 14 19 27 30 33 42 44

    Now as i> j so apply 4th Condition and Swap a[j] & pivot (p);

    So 3rd sub list also got sorted now let us sort 4th sub list

    So all the elements are sorted now

    For 4th Sub List

    a[i] < P i.e. 30 < 27 noSo Check 2nd Condition

    A[j] > P i.e 27 > 27 No

    So apply 3rd Condition

    And swap A[i] & A[j];

  • Sorting Techniques

    60Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Quick Sort Algorithm: 1. Start2. Declare all the required variables and array of size n.3. Read the elements in array.4. After selecting an element as pivot, which is the last index of the array in our case, we divide the array for

    the first time.5. In quick sort, we call this partitioning. It is not simple breaking down of array into 2 sub arrays, but in

    case of partitioning, the array elements are so positioned that all the elements smaller than the pivot willbe on the left side of the pivot and all the elements greater than the pivot will be on the right side of it.

    6. And the pivot element will be at its final sorted position.7. The elements to the left and right, may not be sorted.8. Then we pick subarrays, elements on the left of pivot and elements on the right of pivot, and we

    perform partitioning on them by choosing a pivot in the subarrays.9. Print the sorted array list10. Stop

  • Sorting Techniques

    61Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Quick Sort Pseudo code:

    quickSort(arr[], low, high)

    {

    if (low < high)

    { /* pi is partitioning index, arr[pi] is now at right place */

    pi = partition(arr, low, high);

    quickSort(arr, low, pi - 1); // Before pi

    quickSort(arr, pi + 1, high); // After pi

    }

    }

  • Sorting Techniques

    62Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Quick Sort Pseudo code: partition (arr[], low, high){

    pivot = arr[high];i = (low - 1) // Index of smaller elementfor (j = low; j

  • Sorting Techniques

    63Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Marge Sort: Merge sort is a divide-and-conquer algorithm based on the idea of breaking down a list

    into several sub-lists until each sublist consists of a single element and merging those sublists in a

    manner that results into a sorted list.

    It is implemented using divide & conquer strategy.

    The time complexity for insertion sort is O(nlogn).

    Ex. Let us Sort the given elements 14, 33, 27, 10, 35, 19, 42, 44 Using Marge Sort.

    14 33 27 10 35 19 42 44

  • Sorting Techniques

    64Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    14 33 27 10

    14 33 27 10

    35 19 42 44

    35 19

    35 19

    42 44

    42 44

    14 33 27 10

    14 33 27 10 35 19 42 440 1 2 3 4 5 6 7

    14 33 10 27 19 35 42 44

    14 10 27 33 19 35 42 44

    14 33 27 10 35 19 42 44

  • Sorting Techniques

    65Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Marge Sort Algorithm:1. Start2. Declare all the required variables and array of size n.3. Read the elements in array.4. We take a variable p and store the starting index of our array in this. And we take another

    variable r and store the last index of array in it.5. Then we find the middle of the array using the formula (p + r)/2 and mark the middle index as q, and

    break the array into two subarrays, from p to q and from q + 1 to r index.6. Then we divide these 2 subarrays again, just like we divided our main array and this continues.7. Once we have divided the main array into subarrays with single elements, then we start merging the

    subarrays.8. Print the sorted array list9. Stop

  • Sorting Techniques

    66Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Marge Sort Pseudo Code:

    procedure mergesort( var a as array )

    if ( n == 1 )

    return a

    var l1 as array = a[0] ... a[n/2]

    var l2 as array = a[n/2+1] ... a[n]

    l1 = mergesort( l1 )

    l2 = mergesort( l2 )

    return merge( l1, l2 )

    end procedure

  • Sorting Techniques

    67Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Marge Sort Pseudo Code:procedure merge( var a as array, var b as array )var c as array

    while ( a and b have elements )if ( a[0] > b[0] )

    add b[0] to the end of celse

    add a[0] to the end of cend if

    end whilewhile ( a has elements )

    add a[0] to the end of cend whilewhile ( b has elements )

    add b[0] to the end of cend while

    return cend procedure

  • Sorting Techniques

    68Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik.

    Sr. No.

    Sorting Technique

    Best Case Complexity

    Average Case

    Complexity

    Worst Case Complexity

    1 Bubble Sort O(n2) O(n2) O(n2)

    2 Insertion Sort O(n2) O(n2) O(n2)

    3 Selection Sort O(n2) O(n2) O(n2)

    4 Quick Sort O(nlogn) O(nlogn) O(n2)

    5 Marge Sort O(nlogn) O(nlogn) O(nlogn)

    Comparative Analysis of Sorting algorithms

  • 69Prepared By: Mr. Vipin K. Wani SOCSE, Sandip University, Nashik

    Download ppts from: wanivipin.wordpress.com