Data_Structures_and_Algorithms-2
Data_Structures_and_Algorithms-2
Sorting them according to the first alphabet of the title may also
not be useful, because lots of books start with the word “ The ”.
Since dealing with integers is easier for computers you try to sort
them based on the ISSN of each book.
Why sort?
Recap Observations
Functions and Running time Heuristic
Digression towards Sorting Pseudocode for layman
The link between algorithms and functions Correctness
Pseudocode for Computers
Before I deal with 10K books let me try sorting just 10 books.
Sorting ten books is like having a hand of cards in sorted order.
Why sort?
Recap Observations
Functions and Running time Heuristic
Digression towards Sorting Pseudocode for layman
The link between algorithms and functions Correctness
Pseudocode for Computers
Before I deal with 10K books let me try sorting just 10 books.
Sorting ten books is like having a hand of cards in sorted order.
It is easy to sort the 10 books by simple brute force. But then how
does it help me in sorting 10K books?
Why sort?
Recap Observations
Functions and Running time Heuristic
Digression towards Sorting Pseudocode for layman
The link between algorithms and functions Correctness
Pseudocode for Computers
It is easy to sort the 10 books by simple brute force. But then how
does it help me in sorting 10K books?
If I can sort 11 books using this strategy, then maybe I can extend
that strategy to sort 10K books.
It is easy to sort the 10 books by simple brute force. But then how
does it help me in sorting 10K books?
If I can sort 11 books using this strategy, then maybe I can extend
that strategy to sort 10K books.
Strategy/Pseudocode
Sanity Check
Since we are always inserting a new book in the sorted list of books,
we shall call it insertion sorting algorithm.
for(i : 1 to length(A) − 1)
{
j = i;
while(j > 0 and A[j − 1] > A[j])
{
swap A[j] and A[j − 1];
j = j − 1;
}
}
Why sort?
Recap Observations
Functions and Running time Heuristic
Digression towards Sorting Pseudocode for layman
The link between algorithms and functions Correctness
Pseudocode for Computers
This “while” loop puts the newly added element at its right location
in the sorted part of the array, by swapping elements till the newly
added element finds its right location.
Recap
Algorithms to recurrences
Functions and Running time
Recurrences to functions
Digression towards Sorting
Solving Recurrences by Guessing
The link between algorithms and functions
T (n) = T (n − 1) + n − 1
T (2) = 1
If we solve the recurrence, then we will have the running time of the
algorithm.
In this case we try to guess the value of T (n) for small values of n
and see how it helps.
T (3) = T (2) + 2 = 1 + 2 = 3
T (4) = T (3) + 3 = 6
T (5) = T (4) + 4 = 10
which one can easily prove using induction. Thus T (n) = O(n2 ).
Recap
Algorithms to recurrences
Functions and Running time
Recurrences to functions
Digression towards Sorting
Solving Recurrences by Guessing
The link between algorithms and functions
In the next lecture we will look at general methods used for solving
recurrences.
Recap
Algorithms to recurrences
Functions and Running time
Recurrences to functions
Digression towards Sorting
Solving Recurrences by Guessing
The link between algorithms and functions