Data Structure Lecture No 4 Searching and Sorting in Array
Data Structure Lecture No 4 Searching and Sorting in Array
Searching
Linear Search
Binary Search
Sorting
Bubble Sort
Selection Sort
Insertion Sort
Data Structures
Searching
Important area in Computer Science
5 3 17 60 2 5 3 17 60 2
Key Key
0 1 2 3 4 0 1 2 3 4
5 3 17 60 2 5 3 17 60 2
0 1 2 3 4
Key 5 3 17 60 2 Key
Key
Data Structures
3 6 4 1 9 7 3 2 8
3 6 4 1 9 7 3 2 8
3 6 4 1 9 7 3 2 8
3 6 4 1 9 7 3 2 8
3 6 4 1 9 7 3 2 8
Data Structures
The Disadvantages
if there are 20,000 items in the array and what
you are looking for is in the 19,999th element we
need to search through the entire list.
Data Structures
Sorting in Array
Arrangement of Data in the array is called
sorting.
Sorting in Array
Bubble Sort is sorting tecnique in which each pair
of adjacent elements are compared if they are in
wrong order we swap them.
Bubble Sort
0 1 2 3 4
3 1 15 57 9
Decsending 57 15 9 3 1
Ascending 1 3 9 15 57
Data Structures
Bubble Sort
Array 2 1 3 4 5
Array 2 1 3 4 5
Data Structures
Bubble Sort
Array 2 1 3 4 5
Array 2 1 3 4 5
2 3 1 4 5
Data Structures
Bubble Sort
Array 2 1 3 4 5
Array 2 1 3 4 5
2 3 1 4 5
2 3 4 1 5
Data Structures
Bubble Sort
Array 2 1 3 4 5
Array 2 1 3 4 5
2 3 1 4 5
2 3 4 1 5
2 3 4 5 1
Data Structures
Explaination of Code
Now this program will be explained with proper
example.
Just Consider the array entered by the user.
10 40 13 20 8
Now the program will use the nested loop to
perform sorting. The iterations of outer loop will
specified the number of passes and the inner
loop will specify the number of iterations.
Data Structures
Explaination of Code
At the beginning when the loop begins, the
value of i = 0 , Therefore first pass is started.
Just Consider the array entered by the user.
10 40 13 20 8
In the first pass the value of i = 0 and the inner loop
came into action it will perform 4 iteration and
check the condition of the following block of
statement.
Data Structures
Explaination of Code
Iteration No 1:-
10 40 13 20 8
At first Iteration the value of j =0 and the value of
j = j+1 , Therefore it will compare the values of zero
index and the first index of an array. As we can see
that the value at zero index is smaller than first
index therefore array remains same.
Data Structures
Explaination of Code
Iteration No 1:-
10 40 13 20 8
At first Iteration the value of j =0 and the value of
j = j+1 , Therefore it will compare the values of zero
index and the first index of an array. As we can see
that the value at zero index is smaller than first
index therefore array remains same.