This pdf is for 2nd year student as 2nd year is the crucial period for Btech students and dsa is a tough subject more of this people not get proper notes of it due to thier preparation lacks so i upload an important topic of 2nd year dsa notes about Sorti methods which is a tough topic to be studied
In this share certain methods for example:->
1.Insert sort
2.Quick sort
3.Bubble sort
4.Heap sort
5.Merge sort
Searching algorithms can be categorized as internal or external depending on whether the list resides entirely in main memory or secondary storage. Linear or sequential search is a simple search algorithm that checks each element of a list sequentially until a match is found or the whole list has been searched. Binary search is a faster search algorithm that can only be used on sorted lists. It divides the search space in half at each step to locate the target element.
Sorting algorithms arrange items in a list in a specific order. Common sorting algorithms include selection sort, insertion sort, bubble sort, merge sort, quicksort, and radix sort. Sorting algorithms are analyzed based on their time and space complexity, with most having quadratic time complexity for simple algorithms
The document discusses various sorting algorithms including insertion sort, selection sort, bubble sort, merge sort, and quick sort. It provides detailed explanations of how each algorithm works through examples using arrays or lists of numbers. The key steps of each algorithm are outlined in pseudocode to demonstrate how they sort a set of data in either ascending or descending order.
Introduction to data structures and complexity.pptxPJS KUMAR
The document discusses data structures and algorithms. It defines data structures as the logical organization of data and describes common linear and nonlinear structures like arrays and trees. It explains that the choice of data structure depends on accurately representing real-world relationships while allowing effective processing. Key data structure operations are also outlined like traversing, searching, inserting, deleting, sorting, and merging. The document then defines algorithms as step-by-step instructions to solve problems and analyzes the complexity of algorithms in terms of time and space. Sub-algorithms and their use are also covered.
The document discusses insertion sort and its analysis. It begins by providing an overview of insertion sort, describing how it works to sort a sequence by iteratively inserting elements into their sorted position. It then gives pseudocode for insertion sort and works through an example. Next, it analyzes insertion sort's runtime, showing it is O(n^2) in the worst case and O(n) in the best case. The document concludes by introducing the divide and conquer approach for sorting, which will be covered in the next section on merge sort.
Lecture 02: Preliminaries of Data structureNurjahan Nipa
This document provides an overview of algorithms and data structures. It begins with mathematical notation and complexity analysis. It then discusses control structures like sequence, selection, and iteration. Specific algorithms are described, including finding the largest element in an array, linear search, and the sieve method for finding prime numbers. Floor, ceiling, integer, absolute value, exponent, and logarithm functions are also covered. Overall, the document serves as a lecture outline on fundamental algorithmic concepts.
This document discusses key concepts in algorithm analysis and design including algorithms, data types, specifications vs implementations, time and space complexity analysis, asymptotic notation, and examples. It defines an algorithm as a set of instructions to accomplish a task, and a data type as a collection of objects and operations on those objects. The document covers analyzing time complexity using counting steps and asymptotic notation such as Big O, Omega, and Theta. Examples of matrix and list functions and their analyses are provided.
jn;lm;lkm';m';;lmppt of data structure.pdfVinayNassa3
This document provides an introduction to data structures, algorithms, and complexity analysis. It begins with an overview of data structures and their classification as simple, compound, linear, or non-linear. Common operations on data structures like adding, deleting, and searching elements are described. The document then covers algorithm analysis, asymptotic notation, and examples of recursive and sorting algorithms like bubble sort, selection sort, and their time complexities. Searching techniques like linear, binary, and Fibonacci search are also summarized.
The document provides an introduction to various data structures and algorithms concepts. It discusses different types of data structures like simple, compound, linear and non-linear data structures. It also covers algorithm analysis concepts like time complexity, asymptotic notations and different searching and sorting algorithms like linear search, binary search, bubble sort, selection sort, insertion sort, quick sort and merge sort. It provides pseudocode examples of recursive algorithms like factorial, Fibonacci sequence and towers of Hanoi problem.
The document discusses algorithms and their analysis. It begins by defining an algorithm and key aspects like correctness, input, and output. It then discusses two aspects of algorithm performance - time and space. Examples are provided to illustrate how to analyze the time complexity of different structures like if/else statements, simple loops, and nested loops. Big O notation is introduced to describe an algorithm's growth rate. Common time complexities like constant, linear, quadratic, and cubic functions are defined. Specific sorting algorithms like insertion sort, selection sort, bubble sort, merge sort, and quicksort are then covered in detail with examples of how they work and their time complexities.
Data Structure & Algorithms - Mathematicalbabuk110
This document discusses various mathematical notations and asymptotic analysis used for analyzing algorithms. It covers floor and ceiling functions, remainder function, summation symbol, factorial function, permutations, exponents, logarithms, Big-O, Big-Omega and Theta notations. It provides examples of calculating time complexity of insertion sort and bubble sort using asymptotic notations. It also discusses space complexity analysis and how to calculate the space required by an algorithm.
The document discusses various searching and sorting techniques. It describes linear and binary searching algorithms. It also explains different sorting algorithms like bubble sort, insertion sort, selection sort, and quick sort. Their time complexities and applications are discussed.
introduction to data structures and typesankita946617
A ppt on primitive and non primitive data structures, abstract data types , performance analysis(time and space complexity) using asymptotic notations-(Big O, Theta, Omega), introduction to arrays and their memory representation, types of arrays- single dimension, two dimension, multi-dimensions, basic operations on arrays- insertion, deletion, searching, sorting, sparse matrix and its representation, applications of array
The document discusses and compares several sorting algorithms: bubble sort, selection sort, insertion sort, merge sort, and quick sort. For each algorithm, it provides an explanation of how the algorithm works, pseudocode for the algorithm, and an analysis of the time complexity. The time complexities discussed are:
Bubble sort: O(N^2) in worst case, O(N) in best case
Selection sort: O(N^2)
Insertion sort: O(N^2) in worst case, O(N) in best case
Merge sort: O(N log N)
Quick sort: O(N log N) on average
An algorithm is a finite set of instructions to accomplish a predefined task. Performance of an algorithm is measured by its time and space complexity, with common metrics being big O, big Omega, and big Theta notation. Common data structures include arrays, linked lists, stacks, queues, trees and graphs. Key concepts are asymptotic analysis of algorithms, recursion, and analyzing complexity classes like constant, linear, quadratic and logarithmic time.
The document discusses various sorting algorithms including exchange sorts like bubble sort and quicksort, selection sorts like straight selection sort, and tree sorts like heap sort. For each algorithm, it provides an overview of the approach, pseudocode, analysis of time complexity, and examples. Key algorithms covered are bubble sort (O(n2)), quicksort (average O(n log n)), selection sort (O(n2)), and heap sort (O(n log n)).
The document discusses algorithms, including their definition, properties, analysis of time and space complexity, and examples of recursion and iteration. It defines an algorithm as a finite set of instructions to accomplish a task. Properties include inputs, outputs, finiteness, definiteness, and effectiveness. Time complexity is analyzed using big-O notation, while space complexity considers static and variable parts. Recursion uses function calls to solve sub-problems, while iteration uses loops. Examples include factorial calculation, GCD, and Towers of Hanoi solved recursively.
This document discusses algorithms and their analysis. It begins by defining an algorithm and its key characteristics like being finite, definite, and terminating after a finite number of steps. It then discusses designing algorithms to minimize cost and analyzing algorithms to predict their performance. Various algorithm design techniques are covered like divide and conquer, binary search, and its recursive implementation. Asymptotic notations like Big-O, Omega, and Theta are introduced to analyze time and space complexity. Specific algorithms like merge sort, quicksort, and their recursive implementations are explained in detail.
This document discusses various techniques for optimizing Python code, including:
1. Using the right algorithms and data structures to minimize time complexity, such as choosing lists, sets or dictionaries based on needed functionality.
2. Leveraging Python-specific optimizations like string concatenation, lookups, loops and imports.
3. Profiling code with tools like timeit, cProfile and visualizers to identify bottlenecks before optimizing.
4. Optimizing only after validating a performance need and starting with general strategies before rewriting hotspots in Python or other languages. Premature optimization can complicate code.
In our thesis work, we try to find out the efficiency of several sorting algorithms and generate a comparative report according to performance, based on experimental data size and data order for all algorithm. To do this we have researched, analyzed about 9 different types of well-known sorting algorithms. We convert the algorithms to programmable code, compile and run with different set of data. We keep the sorting algorithm’s description as it is. We give focus on, how the algorithms work, considering their total operation count (assignment count, comparison count) and complexity based on our same data set for all algorithm. We write programming code for each sorting algorithm in C programming language. In our investigation, we have also worked with big and small data for different cases (ordered, pre-ordered, random, disordered) and put their result in different tables. We show the increasing ratio to compare the result. we also show the data in graphical chart for view comparative report in same window. We mark their efficiency with point and ranked them. At last we discussed their result of efficiency in a single table. We modify the merge sort and try to make an improved tri-merge sorting algorithm that is more efficient than marge sort. Theoretically if we divide and conquer with higher number its result is better, some paper exists on it, but to manage the algorithm, there cost lot of operations count. Like, if we consider quadratic divide-conquer, its manage complexly is huge than binary divide-conquer that why we generally use binary merge. We found trimerge is theoretically and practically true based on investigation data set. Tri-marge take some more compare operation for manage and sort when data remain 1 or 2 at last stage, whereas binary merge don’t need such compare. But for big data size tri-merge gain lot of operation count that give significant result that declare tri-merge is more efficient than merge sort algorithm. We also experiment with penta-merge algorithm which give more better result but algorithm and implementation is too complex.
We shall try to define the tri-merge algorithm so that it can be used to implement in any
programming language. It will help students, researchers to use the algorithm, as like we
got the various algorithm structure over the internet.
Quick Sort is a sorting algorithm that partitions an array around a pivot element, recursively sorting the subarrays. It has a best case time complexity of O(n log n) when partitions are evenly divided, and worst case of O(n^2) when partitions are highly imbalanced. While fast, it is unstable and dependent on pivot selection. It is widely used due to its efficiency, simplicity, and ability to be parallelized.
Algorithm And analysis Lecture 03& 04-time complexity.Tariq Khan
This document discusses algorithm efficiency and complexity analysis. It defines key terms like algorithms, asymptotic complexity, Big O notation, and different complexity classes. It provides examples of analyzing time complexity for different algorithms like loops, nested loops, and recursive functions. The document explains that Big O notation allows analyzing algorithms independent of machine or input by focusing on the highest order term as the problem size increases. Overall, the document introduces methods for measuring an algorithm's efficiency and analyzing its time and space complexity asymptotically.
The document discusses divide and conquer algorithms. It explains that divide and conquer algorithms follow three steps: 1) divide the problem into subproblems, 2) conquer the subproblems by solving them recursively, and 3) combine the results to solve the original problem. Binary search, merge sort, and quicksort are provided as examples of divide and conquer algorithms. Binary search divides a sorted array in half at each step to search for a target value. Merge sort divides the array in half, recursively sorts the halves, and then merges the sorted halves. Quicksort chooses a pivot to partition the array into left and right halves, recursively sorts the halves, and returns the fully sorted array.
This document discusses algorithms and their complexity. It provides an example of a linear search algorithm to find a target value in an array. The complexity of this algorithm is analyzed for the worst and average cases. In the worst case, the target is the last element and all n elements must be checked, resulting in O(n) time complexity. On average, about half the elements (n+1)/2 need to be checked, resulting in average time complexity of O(n).
Lecture 02: Preliminaries of Data structureNurjahan Nipa
This document provides an overview of algorithms and data structures. It begins with mathematical notation and complexity analysis. It then discusses control structures like sequence, selection, and iteration. Specific algorithms are described, including finding the largest element in an array, linear search, and the sieve method for finding prime numbers. Floor, ceiling, integer, absolute value, exponent, and logarithm functions are also covered. Overall, the document serves as a lecture outline on fundamental algorithmic concepts.
This document discusses key concepts in algorithm analysis and design including algorithms, data types, specifications vs implementations, time and space complexity analysis, asymptotic notation, and examples. It defines an algorithm as a set of instructions to accomplish a task, and a data type as a collection of objects and operations on those objects. The document covers analyzing time complexity using counting steps and asymptotic notation such as Big O, Omega, and Theta. Examples of matrix and list functions and their analyses are provided.
jn;lm;lkm';m';;lmppt of data structure.pdfVinayNassa3
This document provides an introduction to data structures, algorithms, and complexity analysis. It begins with an overview of data structures and their classification as simple, compound, linear, or non-linear. Common operations on data structures like adding, deleting, and searching elements are described. The document then covers algorithm analysis, asymptotic notation, and examples of recursive and sorting algorithms like bubble sort, selection sort, and their time complexities. Searching techniques like linear, binary, and Fibonacci search are also summarized.
The document provides an introduction to various data structures and algorithms concepts. It discusses different types of data structures like simple, compound, linear and non-linear data structures. It also covers algorithm analysis concepts like time complexity, asymptotic notations and different searching and sorting algorithms like linear search, binary search, bubble sort, selection sort, insertion sort, quick sort and merge sort. It provides pseudocode examples of recursive algorithms like factorial, Fibonacci sequence and towers of Hanoi problem.
The document discusses algorithms and their analysis. It begins by defining an algorithm and key aspects like correctness, input, and output. It then discusses two aspects of algorithm performance - time and space. Examples are provided to illustrate how to analyze the time complexity of different structures like if/else statements, simple loops, and nested loops. Big O notation is introduced to describe an algorithm's growth rate. Common time complexities like constant, linear, quadratic, and cubic functions are defined. Specific sorting algorithms like insertion sort, selection sort, bubble sort, merge sort, and quicksort are then covered in detail with examples of how they work and their time complexities.
Data Structure & Algorithms - Mathematicalbabuk110
This document discusses various mathematical notations and asymptotic analysis used for analyzing algorithms. It covers floor and ceiling functions, remainder function, summation symbol, factorial function, permutations, exponents, logarithms, Big-O, Big-Omega and Theta notations. It provides examples of calculating time complexity of insertion sort and bubble sort using asymptotic notations. It also discusses space complexity analysis and how to calculate the space required by an algorithm.
The document discusses various searching and sorting techniques. It describes linear and binary searching algorithms. It also explains different sorting algorithms like bubble sort, insertion sort, selection sort, and quick sort. Their time complexities and applications are discussed.
introduction to data structures and typesankita946617
A ppt on primitive and non primitive data structures, abstract data types , performance analysis(time and space complexity) using asymptotic notations-(Big O, Theta, Omega), introduction to arrays and their memory representation, types of arrays- single dimension, two dimension, multi-dimensions, basic operations on arrays- insertion, deletion, searching, sorting, sparse matrix and its representation, applications of array
The document discusses and compares several sorting algorithms: bubble sort, selection sort, insertion sort, merge sort, and quick sort. For each algorithm, it provides an explanation of how the algorithm works, pseudocode for the algorithm, and an analysis of the time complexity. The time complexities discussed are:
Bubble sort: O(N^2) in worst case, O(N) in best case
Selection sort: O(N^2)
Insertion sort: O(N^2) in worst case, O(N) in best case
Merge sort: O(N log N)
Quick sort: O(N log N) on average
An algorithm is a finite set of instructions to accomplish a predefined task. Performance of an algorithm is measured by its time and space complexity, with common metrics being big O, big Omega, and big Theta notation. Common data structures include arrays, linked lists, stacks, queues, trees and graphs. Key concepts are asymptotic analysis of algorithms, recursion, and analyzing complexity classes like constant, linear, quadratic and logarithmic time.
The document discusses various sorting algorithms including exchange sorts like bubble sort and quicksort, selection sorts like straight selection sort, and tree sorts like heap sort. For each algorithm, it provides an overview of the approach, pseudocode, analysis of time complexity, and examples. Key algorithms covered are bubble sort (O(n2)), quicksort (average O(n log n)), selection sort (O(n2)), and heap sort (O(n log n)).
The document discusses algorithms, including their definition, properties, analysis of time and space complexity, and examples of recursion and iteration. It defines an algorithm as a finite set of instructions to accomplish a task. Properties include inputs, outputs, finiteness, definiteness, and effectiveness. Time complexity is analyzed using big-O notation, while space complexity considers static and variable parts. Recursion uses function calls to solve sub-problems, while iteration uses loops. Examples include factorial calculation, GCD, and Towers of Hanoi solved recursively.
This document discusses algorithms and their analysis. It begins by defining an algorithm and its key characteristics like being finite, definite, and terminating after a finite number of steps. It then discusses designing algorithms to minimize cost and analyzing algorithms to predict their performance. Various algorithm design techniques are covered like divide and conquer, binary search, and its recursive implementation. Asymptotic notations like Big-O, Omega, and Theta are introduced to analyze time and space complexity. Specific algorithms like merge sort, quicksort, and their recursive implementations are explained in detail.
This document discusses various techniques for optimizing Python code, including:
1. Using the right algorithms and data structures to minimize time complexity, such as choosing lists, sets or dictionaries based on needed functionality.
2. Leveraging Python-specific optimizations like string concatenation, lookups, loops and imports.
3. Profiling code with tools like timeit, cProfile and visualizers to identify bottlenecks before optimizing.
4. Optimizing only after validating a performance need and starting with general strategies before rewriting hotspots in Python or other languages. Premature optimization can complicate code.
In our thesis work, we try to find out the efficiency of several sorting algorithms and generate a comparative report according to performance, based on experimental data size and data order for all algorithm. To do this we have researched, analyzed about 9 different types of well-known sorting algorithms. We convert the algorithms to programmable code, compile and run with different set of data. We keep the sorting algorithm’s description as it is. We give focus on, how the algorithms work, considering their total operation count (assignment count, comparison count) and complexity based on our same data set for all algorithm. We write programming code for each sorting algorithm in C programming language. In our investigation, we have also worked with big and small data for different cases (ordered, pre-ordered, random, disordered) and put their result in different tables. We show the increasing ratio to compare the result. we also show the data in graphical chart for view comparative report in same window. We mark their efficiency with point and ranked them. At last we discussed their result of efficiency in a single table. We modify the merge sort and try to make an improved tri-merge sorting algorithm that is more efficient than marge sort. Theoretically if we divide and conquer with higher number its result is better, some paper exists on it, but to manage the algorithm, there cost lot of operations count. Like, if we consider quadratic divide-conquer, its manage complexly is huge than binary divide-conquer that why we generally use binary merge. We found trimerge is theoretically and practically true based on investigation data set. Tri-marge take some more compare operation for manage and sort when data remain 1 or 2 at last stage, whereas binary merge don’t need such compare. But for big data size tri-merge gain lot of operation count that give significant result that declare tri-merge is more efficient than merge sort algorithm. We also experiment with penta-merge algorithm which give more better result but algorithm and implementation is too complex.
We shall try to define the tri-merge algorithm so that it can be used to implement in any
programming language. It will help students, researchers to use the algorithm, as like we
got the various algorithm structure over the internet.
Quick Sort is a sorting algorithm that partitions an array around a pivot element, recursively sorting the subarrays. It has a best case time complexity of O(n log n) when partitions are evenly divided, and worst case of O(n^2) when partitions are highly imbalanced. While fast, it is unstable and dependent on pivot selection. It is widely used due to its efficiency, simplicity, and ability to be parallelized.
Algorithm And analysis Lecture 03& 04-time complexity.Tariq Khan
This document discusses algorithm efficiency and complexity analysis. It defines key terms like algorithms, asymptotic complexity, Big O notation, and different complexity classes. It provides examples of analyzing time complexity for different algorithms like loops, nested loops, and recursive functions. The document explains that Big O notation allows analyzing algorithms independent of machine or input by focusing on the highest order term as the problem size increases. Overall, the document introduces methods for measuring an algorithm's efficiency and analyzing its time and space complexity asymptotically.
The document discusses divide and conquer algorithms. It explains that divide and conquer algorithms follow three steps: 1) divide the problem into subproblems, 2) conquer the subproblems by solving them recursively, and 3) combine the results to solve the original problem. Binary search, merge sort, and quicksort are provided as examples of divide and conquer algorithms. Binary search divides a sorted array in half at each step to search for a target value. Merge sort divides the array in half, recursively sorts the halves, and then merges the sorted halves. Quicksort chooses a pivot to partition the array into left and right halves, recursively sorts the halves, and returns the fully sorted array.
This document discusses algorithms and their complexity. It provides an example of a linear search algorithm to find a target value in an array. The complexity of this algorithm is analyzed for the worst and average cases. In the worst case, the target is the last element and all n elements must be checked, resulting in O(n) time complexity. On average, about half the elements (n+1)/2 need to be checked, resulting in average time complexity of O(n).
Filters for Electromagnetic Compatibility ApplicationsMathias Magdowski
In this lecture, I explain the fundamentals of electromagnetic compatibility (EMC), the basic coupling model and coupling paths via cables, electric fields, magnetic fields and wave fields. We also look at electric vehicles as an example of systems with many conducted EMC problems due to power electronic devices such as rectifiers and inverters with non-linear components such as diodes and fast switching components such as MOSFETs or IGBTs. After a brief review of circuit analysis fundamentals and an experimental investigation of the frequency-dependent impedance of resistors, capacitors and inductors, we look at a simple low-pass filter. The input impedance from both sides as well as the transfer function are measured.
MODULE 5 BUILDING PLANNING AND DESIGN SY BTECH ACOUSTICS SYSTEM IN BUILDINGDr. BASWESHWAR JIRWANKAR
: Introduction to Acoustics & Green Building -
Absorption of sound, various materials, Sabine’s formula, optimum reverberation time, conditions for good acoustics Sound insulation:
Acceptable noise levels, noise prevention at its source, transmission of noise, Noise control-general considerations
Green Building: Concept, Principles, Materials, Characteristics, Applications
Peak ground acceleration (PGA) is a critical parameter in ground-motion investigations, in particular in earthquake-prone areas such as Iran. In the current study, a new method based on particle swarm optimization (PSO) is developed to obtain an efficient attenuation relationship for the vertical PGA component within the northern Iranian plateau. The main purpose of this study is to propose suitable attenuation relationships for calculating the PGA for the Alborz, Tabriz and Kopet Dag faults in the vertical direction. To this aim, the available catalogs of the study area are investigated, and finally about 240 earthquake records (with a moment magnitude of 4.1 to 6.4) are chosen to develop the model. Afterward, the PSO algorithm is used to estimate model parameters, i.e., unknown coefficients of the model (attenuation relationship). Different statistical criteria showed the acceptable performance of the proposed relationships in the estimation of vertical PGA components in comparison to the previously developed relationships for the northern plateau of Iran. Developed attenuation relationships in the current study are independent of shear wave velocity. This issue is the advantage of proposed relationships for utilizing in the situations where there are not sufficient shear wave velocity data.
Optimize Indoor Air Quality with Our Latest HVAC Air Filter Equipment Catalogue
Discover our complete range of high-performance HVAC air filtration solutions in this comprehensive catalogue. Designed for industrial, commercial, and residential applications, our equipment ensures superior air quality, energy efficiency, and compliance with international standards.
📘 What You'll Find Inside:
Detailed product specifications
High-efficiency particulate and gas phase filters
Custom filtration solutions
Application-specific recommendations
Maintenance and installation guidelines
Whether you're an HVAC engineer, facilities manager, or procurement specialist, this catalogue provides everything you need to select the right air filtration system for your needs.
🛠️ Cleaner Air Starts Here — Explore Our Finalized Catalogue Now!
Department of Environment (DOE) Mix Design with Fly Ash.MdManikurRahman
Concrete Mix Design with Fly Ash by DOE Method. The Department of Environmental (DOE) approach to fly ash-based concrete mix design is covered in this study.
The Department of Environment (DOE) method of mix design is a British method originally developed in the UK in the 1970s. It is widely used for concrete mix design, including mixes that incorporate supplementary cementitious materials (SCMs) such as fly ash.
When using fly ash in concrete, the DOE method can be adapted to account for its properties and effects on workability, strength, and durability. Here's a step-by-step overview of how the DOE method is applied with fly ash.
This presentation provides a detailed overview of air filter testing equipment, including its types, working principles, and industrial applications. Learn about key performance indicators such as filtration efficiency, pressure drop, and particulate holding capacity. The slides highlight standard testing methods (e.g., ISO 16890, EN 1822, ASHRAE 52.2), equipment configurations (such as aerosol generators, particle counters, and test ducts), and the role of automation and data logging in modern systems. Ideal for engineers, quality assurance professionals, and researchers involved in HVAC, automotive, cleanroom, or industrial filtration systems.
2. Searching
Internal Search
The search in which the whole list resides in
the main memory is called internal search.
External Search
The search in which the whole list resides in
the Secondary memory is called external
search.
3. Types of searching Algorithm
Linear or Sequential searching
Binary searching
4. Algorithm For Linear Search
LINEAR(DATA ,N,ITEM,LOC)
1.Set K:=1 and LOC := 0.
2.Repeat Steps 3 and 4 while LOC = 0 and K<=N.
3.If ITEM = DATA[K] ,then Set LOC:= K.
4.Set K:=K+1.
5.If LOC = 0,then:
Write: ITEM is not in the array DATA.
Else:
Write: LOC is the location of ITEM.
6.Exit.
6. Algorithm For Binary Search
BINARY(DATA , LB , UB, ITEM , LOC)
1. Set BEG:= LB, END:=UB and
MID = INT((BEG+END)/2).
2. Repeat Steps 3 and 4 while BEG<=END
and DATA[MID]!=ITEM.
3. If ITEM< DATA[MID] ,then:
Set END := MID-1.
Else:
Set BEG := MID+1.
4. Set MID := INT((BEG+END)/2).
7. Algo. Cont.
5. If DATA[MID]=ITEM, then:
Set LOC:= MID.
Else:
Set LOC:= NULL.
6. Exit.
9. Introduction to Sorting
Sorting: An operation that arranges items into
groups according to specified criterion.
A = { 3 1 6 2 1 3 4 5 9 0 }
A = { 0 1 1 2 3 3 4 5 6 9 }
10. Why Sort and Examples
Consider:
Sorting Books in Library (Dewey system)
Sorting Individuals by Height (Feet and
Inches)
Sorting Movies in Blockbuster
(Alphabetical)
Sorting Numbers (Sequential)
11. Types of Sorting Algorithms
There are many different types of sorting
algorithms, but the primary ones are:
Bubble Sort
Selection Sort
Insertion Sort
Merge Sort
Quick Sort
Radix Sort
Heap Sort
12. Complexity
Most 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.
13. Complexity (cont.)
Complexity in general, measures the
algorithms efficiency in internal factors such
as the time needed to run an algorithm.
External Factors (not related to complexity):
Size of the input of the algorithm
Speed of the Computer
Quality of the Compiler
14. O(n), Ω(n), & Θ(n)
An algorithm or function T(n) is O(f(n))
whenever T(n)'s rate of growth is less than
or equal to f(n)'s rate.
An algorithm or function T(n) is Ω(f(n))
whenever T(n)'s rate of growth is greater
than or equal to f(n)'s rate.
An algorithm or function T(n) is Θ(f(n)) if
and only if the rate of growth of T(n) is equal
to f(n).
15. 1
0
Common Big-Oh’s
Time complexity Example
O(1) constant Adding to the front of a linked list
O(log N) log Finding an entry in a sorted array
O(N) linear Finding an entry in an unsorted array
O(N log N) n-log-n Sorting n items by ‘divide-and-conquer’
O(N2
) quadratic Shortest path between two nodes in a graph
O(N3
) cubic Simultaneous linear equations
(Binary)
Finding 8:
9
50
22
21
8
5
1
(Linear)
Finding 8:
9
50
22
21
8
5
1
Front
Initial:
Final:6
3
http://www.cs.sjsu.edu/faculty/lee/cs146/23FL3Complexity.ppt
17. Time Efficiency
How do we improve the time efficiency of a
program?
The 90/10 Rule
90% of the execution time of a program is spent in
executing 10% of the code
So, how do we locate the critical 10%?
software metrics tools
global counters to locate bottlenecks (loop
executions, function calls)
18. Time Efficiency Improvements
Possibilities (some better than others!)
Move code out of loops that does not belong
there (just good programming!)
Remove any unnecessary I/O operations (I/O
operations are expensive time-wise)
Code so that the compiled code is more
efficient
Moral - Choose the most appropriate
algorithm(s) BEFORE program implementation
20. Selection Sort
MIN(A,K,N,LOC)
1. Set MIN = A[K] and LOC = K
2. Repeat for J=K+1, K+2 ……… N
If MIN>A[J], then Set MIN = A[J]
and LOC = J
End of Loop
3. Return
21. Selection Sort
SELECTION (A,N)
1. Repeat steps 2 and 3 for K=1, 2 ……… N-1
2. Call MIN(A,K,N,LOC)
3. Set TEMP = A[K]
A[K] = A[LOC]
A[LOC] = TEMP
End of Step 1 Loop
4. Exit
22. Complexity of the Selection Sort
Worst Case
O(n2)
Average Case
O(n2)
25. Insertion Sort
INSERTION (A,N)
1. Set A[0] = - ∞
2. Repeat Steps 3 to 5 for K = 2,3,….N
3. Set TEMP = A[K] and PTR = K-1
4. Repeat while TEMP<A[PTR]
1. Set A[PTR+1] = A[PTR]
2. Set PTR = PTR – 1
End of Loop
5. Set A[PTR+1] = TEMP
End of Step 2 Loop
6. Return
26. Complexity of the Insertion Sort
Worst Case
O(n2)
Average Case
O(n2)
28. Algorithm for BUBBLE SORT
BUBBLE(DATA , N)
1.Repeat Steps 2 and 3 for K=1 to N-1.
2.Set PTR := 1. // Initialize pass pointer PTR
3.Repeat while PTR<=N-K.
a) If DATA[PTR]>DATA[PTR+1],then
Interchange DATA[PTR] and
DATA[PTR+1]
b) Set PTR:= PTR+1
4. Exit.
33. MERGE SORT
MERGE_SORT (A, BEG, END)
1. IF BEG<ENG
SET MID=(BEG+END)/2
CALL MERGE_SORT (A, BEG, MID)
CALL MERGE_SORT (A, MID+1, END)
MERGE(A, BEG, MID, END)
2. END
34. MERGE(A, BEG, MID, END)
1. SET I = BEG, J=MID+1, INDEX=0
2. REPEAT WHILE (I<=MID) AND (J<=END)
IF A[I]<A[J], THEN
SET TEMP [INDEX] = A[I]
SET I=I+1
ELSE
SET TEMP [INDEX]=A[J]
SET J=J+1
SET INDEX = INDEX +1
35. 3. IF I>MID THEN
REPEAT WHILE J<=END
SET TEMP [INDEX]=A[J]
SET INDEX=INDEX+1
SET J=J+1
ELSE
REPEAT WHILE I<=MID
SET TEMP[INDEX]=A[I]
SET INDEX = INDEX +1
SET I=I+1
36. 4. SET K=0
5. REPEAT WHILE K<INDEX
SET A[K]=TEMP[K]
SET K=K+1
6. END
37. QUICKSORT
Starting with first element of list do the operations
1. Compare from right to find element less than
selected and interchange
2. Compare from left to find element greater than
selected and interchange
44,33,11,55,77,90,40,60,33,22,88,66
22,33,11,55,77,90,40,60,99,44,88,66
22,33,11,44,77,90,40,60,99,55,88,66
22,33,11,40,77,90,44,60,99,55,88,66
22,33,11,40,44,90,77,60,99,55,88,66
22,33,11,40,44,90,77,60,99,55,88,66
Two Sublists are created:
Before 44 and After 44
38. 38
Quick Sort Algorithm
QUICK (A, N, BEG, END, LOC)
1. [Initialize] Set LEFT= BEG, RIGHT=END and LOC=BEG
2. [Scan from right to left]
a) Repeat while A[LOC]<=A[RIGHT] and LOC != RIGHT
RIGHT=RIGHT-1
[End of loop]
b) If LOC=RIGHT then : Return
c) If A[LOC]>A[RIGHT] then
i) Interchange A[LOC] and A[RIGHT]
ii) Set LOC=RIGHT
iii) Go to step 3
[End of If structure]
39. 3. [Scan from left to right]
a) Repeat while A[LEFT]<=A[LOC] and LEFT!=LOC
LEFT=LEFT+1
[End of loop]
b) If LOC=LEFT, then Return
c) If A[LEFT]>A[LOC], then
i) Interchange A[LEFT] and A[LOC]
ii) Set LOC=LEFT
iii) Goto step 2
[End of If structure]
39
40. (QuickSort) This algorithm sorts an array A with N elements.
1. TOP=NULL
2. If N>1 then TOP=TOP+1, LOWER[1]=1, UPPER[1]=N
3. Repeat Steps 4 to 7 while TOP!=NULL
4. Set BEG=LOWER[TOP], END=UPPER[TOP]
TOP=TOP-1
5. Call QUICK(A, N, BEG, END, LOC)
6. If BEG<LOC-1 then
TOP=TOP+1, LOWER[TOP]=BEG, UPPER[TOP]=LOC-1
[End of If structure]
7. If LOC+1<END then
TOP=TOP+1, LOWER[TOP]=LOC+1, UPPER[TOP]=END
[End of If Structure]
[End of Step 3 loop]
8. Exit
40
43. Radix Sort (or Bucket Sort)
It is a non comparative integer sorting
algorithm
It sorts data with integer keys by grouping
keys by the individual digits which share the
same significant position and value.
A positional notation is required, but because
integers can represent strings of characters
(e.g., names or dates) and specially
formatted floating point numbers, Radix sort
is not limited to integers.
44. Radix Sort Algorithm
Let A be an array with N elements in the memory
Find the largest element of the array
Find the total no. of digits ‘NUM’ in the largest
element
Repeat Steps 4, 5 for PASS = 1 to NUM
Initialize buckets (0 to 9 for numbers, A to Z for
Characters)
For i = 0 to (N-1)
Set DIGIT = Obtain digit number PASS of A [i]
Put A [i] in bucket number DIGIT
End of for loop
5. Collect all numbers from the bucket in order
6. Exit