SlideShare a Scribd company logo
1 of 23
COMPUTER
PROGRAMMING
Lecture 05
Prepared By Mr. V. S. Patil Dept (CSE)/AEC
Unit 1 Problem Solving
Syllabus
Prepared By Mr. V. S. Patil Dept (CSE)/AEC
2
1
Understand insertion sort algorithms
Bubble sort algorithms
Objectives
Sorting
•Sorting is defined as arranging the record in some logical manner.
•Sorted = ordered based on a particular way.
•Generally, collections of data are presented in a sorted manner.
•Examples of Sorting:
–Words in a dictionary are sorted (and case distinctions are ignored).
–Files in a directory are often listed in sorted order.
–The index of a book is sorted (and case distinctions are ignored).
•InsertIon sort
•BuBBle sort
Insertion sort
•Suppose an array A with n element A[1], A[2], A[1],…A[n] is
in the memory.
The insertion sort algorithm scans A from A[1] to A[n]
inserting each element A[k] into its proper position in the
previously Sorted sub array A[1], A[2],…. A[k-1].
Cont…….
•That is:-
Pass 1: A[1] by itself is trivially sorted.
Pass 2: A[2] is inserted either before or after A[1] so
that : A[1], A[2] is sorted.
Pass 3: A[3] is inserted into its proper place in A[1],
A[2], that is before A[1], between A[1] and A[2], or after
A[2] so that A[1] ,A[2], A[3] is sorted.
Continue..
Pass 4: A[4] is inserted into its proper place in A[1]
,A[2] ,A[3] so that A[1] A[2] A[3] A[4] is sorted.
.
.
.
Pass [n]: A[n] is inserted into its proper place in A[1],
A[2], A[3]…….. A[n-1] so that A[1], A[2]…….. A[n] is
sorted.
2
1
3
4
6
5
Set A[0]= - ~ [Initialize sentinel element]
Repeat step 3 to 5 for k=2,3,…n
Set TEMP:=A[k] and PTR:=k-1
Repeat while TEMP< A[PTR]
a)Set A[PTR+1]:=A[PTR] [MOVES ELEMENT
FORWARD]
b)Set PTR:= PTR-1 [End of loop]
Set A[PTR+1]:=TEMP [Insert element in to its proper place]
ReturnStop
Algorithm Development
(Insertion sort) INSERTION (A,N)
This algorithm sort the array A with N element.
Example: sorting numberes
23 17 45 18 12 22
Consider the list of unsorted numbersConsider the list of unsorted numbers
Example: sorting numbered cards
23 17 45 18 12 22
1 2 6543
Unsorted List
Example: sorting numbered cards
23 17 45 18 12 22
1 2 6543
1 2 6543
Unsorted List
Sorted List
Example: sorting numbered cards
23
17 45 18 12 22
1 2 6543
1 2 6543
Unsorted List
Sorted List
Example: sorting numbered cards
2317
45 18 12 22
1 2 6543
1 2 6543
Unsorted List
Sorted List
Example: sorting numbered cards
2317 45
18 12 22
1 2 6543
1 2 6543
Unsorted List
Sorted List
Example: sorting numbered cards
2317 4518
12 22
1 2 6543
1 2 6543
Unsorted List
Sorted List
Example: sorting numbered cards
1812 2217 23 45
1 2 6543
1 2 6543
Sorted List
Bubble Sort: Idea
• Idea: bubble in water.
Bubble in water moves upward. Why?
• How?
–When a bubble moves upward, the water from above will
move downward to fill in the space left by the bubble.
Bubble sort
Compare each element (except the last two) with its
neighbor to the right
If they are out of order, swap them
This puts the second largest element next to last
The last two elements are now in their correct and final
places
Compare each element (except the last one) with its neighbor
to the right
If they are out of order, swap them
This puts the largest element at the very end
The last element is now in the correct and final place
Compare each element (except the last three) with its
neighbor to the right
Continue as above until you have no unsorted elements on
the left
Continue..
Example of bubble sort
7 2 8 5 4
2 7 8 5 4
2 7 8 5 4
2 7 5 8 4
2 7 5 4 8
2 7 5 4 8
2 7 5 4 8
2 5 7 4 8
2 5 4 7 8
2 5 4 7 8
2 5 4 7 8
2 4 5 7 8
2 4 5 7 8
2 4 5 7 8
(done)
•Let A be the list of N number. Sorting A refers to the
operation of re-arranging the element of A so that they
are in increasing order,
So that :- A[1] <A[2]< A[3] <……….A[N].
•For example suppose A originally the list.
8, 4, 19, 2, 7, 13, 5, 16.
After sorting A the list is
2, 4, 5, 7, 8, 13, 16, 19.
Bubble sort:-
1
1.Repeat
step 2and
3for k=1 to
N-1
2
Set PTER=1
[Initialize
pass
pointer
PTER].
3
Repeat
while
PTR<=N-K
[Executes
pass].
.
4
a)If DATA
[PTR]>DATA[PTR+1]
Interchange
DATA[PTER]
and DATA[PTR+1]
[End of if structure]
5
b)Set
PTR:=PTR+1
[End of inner
loop]
[End of step 1
outer loop]
6
Exit
Return
Stop
(Bubble sort) BUBBLE(Data, N)
Here DATA is an array with N element. This algorithm sort the
elements in DATA.
Question Bank
1. What is mean by sorting?
2. Draw the flowchart for insertion sort.
3. Write process to sort following list using Bubble sort.
12 45 34 67 98 42 23 7 87

More Related Content

What's hot

Python Programming | JNTUA | UNIT 3 | Lists |
Python Programming | JNTUA | UNIT 3 | Lists | Python Programming | JNTUA | UNIT 3 | Lists |
Python Programming | JNTUA | UNIT 3 | Lists | FabMinds
 
Array 2
Array 2Array 2
Array 2Abbott
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sortingKaushal Shah
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3smruti sarangi
 
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortData Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortManishPrajapati78
 
SORTTING IN LINEAR TIME - Radix Sort
SORTTING IN LINEAR TIME - Radix SortSORTTING IN LINEAR TIME - Radix Sort
SORTTING IN LINEAR TIME - Radix SortDevanshu Taneja
 
Log properties
Log propertiesLog properties
Log propertiesmlobrien15
 
Log properties
Log propertiesLog properties
Log propertiesmlobrien15
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesjayavignesh86
 
Algorithm designing using divide and conquer algorithms
Algorithm designing using divide and conquer algorithmsAlgorithm designing using divide and conquer algorithms
Algorithm designing using divide and conquer algorithmsSiddhantShelake
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Hossain Md Shakhawat
 
Module 01 Stack and Recursion
Module 01 Stack and RecursionModule 01 Stack and Recursion
Module 01 Stack and RecursionTushar B Kute
 

What's hot (20)

Linked list
Linked listLinked list
Linked list
 
Python Programming | JNTUA | UNIT 3 | Lists |
Python Programming | JNTUA | UNIT 3 | Lists | Python Programming | JNTUA | UNIT 3 | Lists |
Python Programming | JNTUA | UNIT 3 | Lists |
 
6.queue
6.queue6.queue
6.queue
 
Array 2
Array 2Array 2
Array 2
 
Sorting
SortingSorting
Sorting
 
Counting sort
Counting sortCounting sort
Counting sort
 
Arrays
ArraysArrays
Arrays
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3
 
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortData Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge Sort
 
SORTTING IN LINEAR TIME - Radix Sort
SORTTING IN LINEAR TIME - Radix SortSORTTING IN LINEAR TIME - Radix Sort
SORTTING IN LINEAR TIME - Radix Sort
 
Log properties
Log propertiesLog properties
Log properties
 
Log properties
Log propertiesLog properties
Log properties
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Algorithm designing using divide and conquer algorithms
Algorithm designing using divide and conquer algorithmsAlgorithm designing using divide and conquer algorithms
Algorithm designing using divide and conquer algorithms
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
 
Module 01 Stack and Recursion
Module 01 Stack and RecursionModule 01 Stack and Recursion
Module 01 Stack and Recursion
 
Presentation
PresentationPresentation
Presentation
 

Viewers also liked

COMPUTER PROGRAMMING UNIT 1 Lecture 6
COMPUTER PROGRAMMING UNIT 1 Lecture 6COMPUTER PROGRAMMING UNIT 1 Lecture 6
COMPUTER PROGRAMMING UNIT 1 Lecture 6Vishal Patil
 
Database system by VISHAL PATIL
Database system by VISHAL PATILDatabase system by VISHAL PATIL
Database system by VISHAL PATILVishal Patil
 
COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4Vishal Patil
 
COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1Vishal Patil
 
Important Questions of fourier series with theoretical study Engg. Mathem...
Important Questions  of  fourier series with theoretical study   Engg. Mathem...Important Questions  of  fourier series with theoretical study   Engg. Mathem...
Important Questions of fourier series with theoretical study Engg. Mathem...Mohammad Imran
 
COMPUTER PROGRAMMING UNIT 1 Lecture 3
COMPUTER PROGRAMMING UNIT 1 Lecture 3COMPUTER PROGRAMMING UNIT 1 Lecture 3
COMPUTER PROGRAMMING UNIT 1 Lecture 3Vishal Patil
 
COMPUTER PROGRAMMING UNIT 1 Lecture 2
COMPUTER PROGRAMMING UNIT 1 Lecture 2COMPUTER PROGRAMMING UNIT 1 Lecture 2
COMPUTER PROGRAMMING UNIT 1 Lecture 2Vishal Patil
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting AlgorithmsShivam Singh
 

Viewers also liked (10)

COMPUTER PROGRAMMING UNIT 1 Lecture 6
COMPUTER PROGRAMMING UNIT 1 Lecture 6COMPUTER PROGRAMMING UNIT 1 Lecture 6
COMPUTER PROGRAMMING UNIT 1 Lecture 6
 
Database system by VISHAL PATIL
Database system by VISHAL PATILDatabase system by VISHAL PATIL
Database system by VISHAL PATIL
 
COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4
 
COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1
 
Important Questions of fourier series with theoretical study Engg. Mathem...
Important Questions  of  fourier series with theoretical study   Engg. Mathem...Important Questions  of  fourier series with theoretical study   Engg. Mathem...
Important Questions of fourier series with theoretical study Engg. Mathem...
 
COMPUTER PROGRAMMING UNIT 1 Lecture 3
COMPUTER PROGRAMMING UNIT 1 Lecture 3COMPUTER PROGRAMMING UNIT 1 Lecture 3
COMPUTER PROGRAMMING UNIT 1 Lecture 3
 
COMPUTER PROGRAMMING UNIT 1 Lecture 2
COMPUTER PROGRAMMING UNIT 1 Lecture 2COMPUTER PROGRAMMING UNIT 1 Lecture 2
COMPUTER PROGRAMMING UNIT 1 Lecture 2
 
Computer Programming- Lecture 5
Computer Programming- Lecture 5 Computer Programming- Lecture 5
Computer Programming- Lecture 5
 
Sucess
SucessSucess
Sucess
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 

Similar to COMPUTER PROGRAMMING UNIT 1 Lecture 5

Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptxssuserd602fd
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHISowmya Jyothi
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & AlgorithumsAin-ul-Moiz Khawaja
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data StructureMandeep Singh
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsAakash deep Singhal
 
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14sumitbardhan
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structurestudent
 
Theory of Computation "Chapter 1, introduction"
Theory of Computation "Chapter 1, introduction"Theory of Computation "Chapter 1, introduction"
Theory of Computation "Chapter 1, introduction"Ra'Fat Al-Msie'deen
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptxchouguleamruta24
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing conceptsLJ Projects
 

Similar to COMPUTER PROGRAMMING UNIT 1 Lecture 5 (20)

Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data Structure
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
 
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
 
sorting1.pptx
sorting1.pptxsorting1.pptx
sorting1.pptx
 
Sorting2
Sorting2Sorting2
Sorting2
 
2.DS Array
2.DS Array2.DS Array
2.DS Array
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structure
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
 
DSA.pdf
DSA.pdfDSA.pdf
DSA.pdf
 
LEC3-DS ALGO(updated).pdf
LEC3-DS  ALGO(updated).pdfLEC3-DS  ALGO(updated).pdf
LEC3-DS ALGO(updated).pdf
 
CSPC/ PPS Sorting methods
CSPC/ PPS Sorting methodsCSPC/ PPS Sorting methods
CSPC/ PPS Sorting methods
 
arrays
arraysarrays
arrays
 
CSE225_LEC3 (1).pptx
CSE225_LEC3 (1).pptxCSE225_LEC3 (1).pptx
CSE225_LEC3 (1).pptx
 
queue_final.pptx
queue_final.pptxqueue_final.pptx
queue_final.pptx
 
Theory of Computation "Chapter 1, introduction"
Theory of Computation "Chapter 1, introduction"Theory of Computation "Chapter 1, introduction"
Theory of Computation "Chapter 1, introduction"
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
 

Recently uploaded

Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...ranjana rawat
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 

Recently uploaded (20)

Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 

COMPUTER PROGRAMMING UNIT 1 Lecture 5

  • 1. COMPUTER PROGRAMMING Lecture 05 Prepared By Mr. V. S. Patil Dept (CSE)/AEC
  • 2. Unit 1 Problem Solving Syllabus Prepared By Mr. V. S. Patil Dept (CSE)/AEC
  • 3. 2 1 Understand insertion sort algorithms Bubble sort algorithms Objectives
  • 4. Sorting •Sorting is defined as arranging the record in some logical manner. •Sorted = ordered based on a particular way. •Generally, collections of data are presented in a sorted manner. •Examples of Sorting: –Words in a dictionary are sorted (and case distinctions are ignored). –Files in a directory are often listed in sorted order. –The index of a book is sorted (and case distinctions are ignored). •InsertIon sort •BuBBle sort
  • 5. Insertion sort •Suppose an array A with n element A[1], A[2], A[1],…A[n] is in the memory. The insertion sort algorithm scans A from A[1] to A[n] inserting each element A[k] into its proper position in the previously Sorted sub array A[1], A[2],…. A[k-1].
  • 6. Cont……. •That is:- Pass 1: A[1] by itself is trivially sorted. Pass 2: A[2] is inserted either before or after A[1] so that : A[1], A[2] is sorted. Pass 3: A[3] is inserted into its proper place in A[1], A[2], that is before A[1], between A[1] and A[2], or after A[2] so that A[1] ,A[2], A[3] is sorted.
  • 7. Continue.. Pass 4: A[4] is inserted into its proper place in A[1] ,A[2] ,A[3] so that A[1] A[2] A[3] A[4] is sorted. . . . Pass [n]: A[n] is inserted into its proper place in A[1], A[2], A[3]…….. A[n-1] so that A[1], A[2]…….. A[n] is sorted.
  • 8. 2 1 3 4 6 5 Set A[0]= - ~ [Initialize sentinel element] Repeat step 3 to 5 for k=2,3,…n Set TEMP:=A[k] and PTR:=k-1 Repeat while TEMP< A[PTR] a)Set A[PTR+1]:=A[PTR] [MOVES ELEMENT FORWARD] b)Set PTR:= PTR-1 [End of loop] Set A[PTR+1]:=TEMP [Insert element in to its proper place] ReturnStop Algorithm Development (Insertion sort) INSERTION (A,N) This algorithm sort the array A with N element.
  • 9. Example: sorting numberes 23 17 45 18 12 22 Consider the list of unsorted numbersConsider the list of unsorted numbers
  • 10. Example: sorting numbered cards 23 17 45 18 12 22 1 2 6543 Unsorted List
  • 11. Example: sorting numbered cards 23 17 45 18 12 22 1 2 6543 1 2 6543 Unsorted List Sorted List
  • 12. Example: sorting numbered cards 23 17 45 18 12 22 1 2 6543 1 2 6543 Unsorted List Sorted List
  • 13. Example: sorting numbered cards 2317 45 18 12 22 1 2 6543 1 2 6543 Unsorted List Sorted List
  • 14. Example: sorting numbered cards 2317 45 18 12 22 1 2 6543 1 2 6543 Unsorted List Sorted List
  • 15. Example: sorting numbered cards 2317 4518 12 22 1 2 6543 1 2 6543 Unsorted List Sorted List
  • 16. Example: sorting numbered cards 1812 2217 23 45 1 2 6543 1 2 6543 Sorted List
  • 17. Bubble Sort: Idea • Idea: bubble in water. Bubble in water moves upward. Why? • How? –When a bubble moves upward, the water from above will move downward to fill in the space left by the bubble.
  • 18. Bubble sort Compare each element (except the last two) with its neighbor to the right If they are out of order, swap them This puts the second largest element next to last The last two elements are now in their correct and final places
  • 19. Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them This puts the largest element at the very end The last element is now in the correct and final place Compare each element (except the last three) with its neighbor to the right Continue as above until you have no unsorted elements on the left Continue..
  • 20. Example of bubble sort 7 2 8 5 4 2 7 8 5 4 2 7 8 5 4 2 7 5 8 4 2 7 5 4 8 2 7 5 4 8 2 7 5 4 8 2 5 7 4 8 2 5 4 7 8 2 5 4 7 8 2 5 4 7 8 2 4 5 7 8 2 4 5 7 8 2 4 5 7 8 (done)
  • 21. •Let A be the list of N number. Sorting A refers to the operation of re-arranging the element of A so that they are in increasing order, So that :- A[1] <A[2]< A[3] <……….A[N]. •For example suppose A originally the list. 8, 4, 19, 2, 7, 13, 5, 16. After sorting A the list is 2, 4, 5, 7, 8, 13, 16, 19. Bubble sort:-
  • 22. 1 1.Repeat step 2and 3for k=1 to N-1 2 Set PTER=1 [Initialize pass pointer PTER]. 3 Repeat while PTR<=N-K [Executes pass]. . 4 a)If DATA [PTR]>DATA[PTR+1] Interchange DATA[PTER] and DATA[PTR+1] [End of if structure] 5 b)Set PTR:=PTR+1 [End of inner loop] [End of step 1 outer loop] 6 Exit Return Stop (Bubble sort) BUBBLE(Data, N) Here DATA is an array with N element. This algorithm sort the elements in DATA.
  • 23. Question Bank 1. What is mean by sorting? 2. Draw the flowchart for insertion sort. 3. Write process to sort following list using Bubble sort. 12 45 34 67 98 42 23 7 87