Chapter 1 Overview
•Introduction to the Data Structures
• Concepts and need of Data Structure
• Abstract Data Type
• Type of Data Structures (i) Linear Data Structures (ii) Non Linear Data Structures
• Algorithm Complexity: (i) Time (ii) Space
• Operations on Data Structures :(i) Traversing (ii)Searching (iii)Insertion (iv)Deletion
(v)sorting
3.
SEARCHING AND SORTING
•How do you find someone’s phone number in the phone book?
• How do you find your keys when you’ve misplaced them?
• If a deck of cards has less than 52 cards, how do you determine which card is
missing?
• Searching for items and sorting through items are tasks that we do everyday.
• Searching and sorting are also common tasks in computer programs.
• We search for all occurrences of a word in a file in order to replace it with another
word.We sort the items on a list into alphabetical or numerical order.
4.
LEARNING OBJECTIVE
• Searchingand sorting are two of the most common operations in computer science.
• Searching refers to finding the position of a value in a collection of values.
• Sorting refers to arranging data in a certain order. The two commonly used orders
are
• numerical order and alphabetical order.
• In this chapter, we will discuss the different techniques of searching and sorting
arrays of numbers or characters.
5.
INTRODUCTION TO SEARCHING
•Searching means to find whether a particular value is present in an array or not.
• If the value is present in the array, then searching is said to be successful and the
searching process gives the location of that value in the array.
• However, if the value is not present in the array, the searching process displays an
appropriate message and in this case searching is said to be unsuccessful.
• There are two popular methods for searching the array elements:
1. linear search
2. binary search
6.
LINEAR SEARCH
• Linearsearch, also called as sequential search, is a very simple method used for
searching an array for a particular value.
• It works by comparing the value to be searched with every element of the array one
by one in a sequence until a match is found.
• Linear search is mostly used to search an
• unordered list of elements (array in which data elements are not sorted). For example,
if an array
• A[] is declared and initialized as,
• int A[] = {10, 8, 2, 7, 3, 4, 9, 1, 6, 5}
7.
WORKING OF LINEARSEARCH
• Comparison starts with first element from the list and continues till number is found or
comparison reaches to the last element of the list. As each element is checked with search
element, the process of searching requires more time.
• Time complexity of linear search is O (n) where n indicates number of elements in list.
• Example:- Using array representation Input list 10, 20, 30, 40, 50 and Search element
30
10 20 30 40 50
0 1 2 3 4
Value
index
9.
EXAMPLE
0 1 23 4 5 6 7 8
Element 8 is Found at
Location 6
13.
WRITE A PROGRAMTO SEARCH AN ELEMENT IN AN ARRAY USING
THE LINEAR SEARCH TECHNIQUE.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10], i, item,n;
clrscr();
printf("nEnter number of elements of an array:n");
scanf("%d",&n);
printf("nEnter elements: n");
for (i=0; i<n; i++)
scanf("%d", &a[i]);
printf("nEnter item to search: ");
scanf("%d", &item);
14.
for (i=0; i<n;i++)
{
if (item == a[i])
{
printf("n%d found at location %d", item,i);
break;
}
}
if (i ==n)
printf("n%d does not exist.“,item);
getch();
}
WRITE A PROGRAMTO SEARCH AN ELEMENT IN AN STRING ARRAY USING BINARY
SEARCH.
19.
WORKING OF BINARYSEARCH
Binary search is performed only on sorted array.
Search method starts with calculating mid position from an array and compare the mid
position element with the search element.
If a match is found then the search process ends otherwise divide the list into 2 parts.
First part contains all the numbers less than mid position element and second part contains
all the numbers greater than mid position element.
Then select one of the part depending on search element is less or greater than mid
position element and calculate mid position for selected part.
Again compare mid position element with search element.
The binary search performs comparison and division task the element is found or division
of list gives one element for comparison.
BINARY SEARCH
6 1217 23 38 45 77 84 90
0 1 3
2 5
4 7
6 8
index
Array
Elements
low high
Key(Search Element)
Case 1 : key==a[mid]
Case 2 : key >a[mid]
Case 3 : key < a[mid]
mid
1. Binary search starts by comparing key with middle position element.
22.
BINARY SEARCH
6 1217 23 38 45 77 84 90
0 1 3
2 5
4 7
6 8
index
Array
Elements
low high
Key(Search Element)
mid
Case 1 : not satisfied
Key==a[mid]
45!=38
Case 2 : satisfied
key>a[mid]
45>38
low high mid
low = mid + 1
low = 4 + 1
low = 5
mid = (low + high )/2
mid = (5 + 8)/2
mid = 13/2=6.5
mid =6
0 8 4
5 8 6
Step 1
23.
BINARY SEARCH
6 1217 23 38 45 77 84 90
0 1 3
2 5
4 7
6 8
index
Array
Elements
low
high
Key(Search Element)
mid
Case 1 : not satisfied
Key==a[mid]
45!=77
Case 2 : not satisfied
key>a[mid]
45 not greater than 77
Case 3 : satisfied
key < a[mid]
45 < 77
low high mid
high = mid - 1
high = 6 - 1
high = 5
mid = (low + high )/2
mid = (5 + 5)/2
mid = 10/2=5
mid =5
0 8 4
5 8 6
5 5 5
Step 2
24.
BINARY SEARCH
6 1217 23 38 45 77 84 90
0 1 3
2 5
4 7
6 8
index
Array
Elements
low
high
Key(Search Element)
mid
Case 1 : satisfied
Key==a[mid]
45==45
low high mid
45 is found at location 5
0 8 4
5 8 6
5 5 5
Step 3
25.
Example :
Find theposition of element 21 using binary search method in array ‘A’ given below:
A={11,5,21,3,29,17,2,45}
Ans :
Given Array
Sorted Array for input:
Key element to be searched=21
11 5 21 3 29 17 2 45
2 3 5 11 17 21 29 45
26.
• Step1
2 35 11 17 21 29 45
0 1 2 3 4 5 6 7
Array Elements
index
low high
mid=(low+high)/2
mid=(0+7)/2=3.5
mid=3
low high mid
0 7 3
mid
Case 1 : not satisfied
Key==a[mid]
21!=11
Case 2 : satisfied
key>a[mid]
21>11
low = mid + 1
low = 3 + 1
low = 4
27.
• Step 2
lowhigh mid
0 7 3
4 7 5
2 3 5 11 17 21 29 45
0 1 2 3 4 5 6 7
Array Elements
index
low
high
mid
mid=(low+high)/2
mid=(4+7)/2=5.5
mid=5
Case 1 : satisfied
Key==a[mid]
21==21
21 is found at location 5
WRITE A PROGRAMTO SEARCH AN ELEMENT IN AN ARRAY USING BINARY SEARCH.
#include <stdio.h>
#include <conio.h>
void main()
{
int arr[10], key, i, n , low,high,mid;
clrscr ();
printf("n Enter the number of elements in the array: ");
scanf ("%d", &n);
printf (" n Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
printf("n Enter the number that has to be searched: " );
scanf ("%d", &key);
low = 0, high = n-1;
mid=(low+high)/2;
30.
while (low <=high)
{
if (arr[mid] == key)
{
printf("n %dis present in the array at position = %d", key, mid);
break;
}
else if (key> arr[mid])
{
low=mid+1;
}
else
{
high=mid-1;
}
mid=(low+high)/2;
}
31.
if ( low> high)
{
printf("n %d DOES NOT EXIST IN THE ARRAY",key);
}
getch();
}
INTRODUCTION TO SORTING
•Sorting means arranging the elements of an array so that they are placed in
some order which may be either ascending or descending.
• There are two types of sorting:
Internal sorting which deals with sorting the data stored in the
computer’s memory
External sorting which deals with sorting the data stored in files.
External sorting is applied when there is voluminous data that cannot be stored
in the memory.
BUBBLE SORT
Bubble sortbegins with , 0th
element is compared with 1st
element . If 0th
element is greater than 1st
then they are exchanged(swap).
Next 1st
element is compared with 2nd
element . If 1st
element is greater
than 2nd
then they are exchanged(swap).
In this way all the elements are compared with next element and are
exchanged if required.
At the end of pass 1 largest element gets placed at the last position.
In second pass same process is repeated and second largest element is
placed at second last position.
After all passes the list becomes a sorted list.
39.
EXAMPLE
List : 2517 31 13 2
Index (0) (1) (2) (3) (4)
Pass 1 :
25 17 31 13 2 25 is greater than 17 so swap
(0) (1) (2) (3) (4)
17 25 31 13 2 25 is not greater than 31 so no swap
17 25 31 13 2 31 is greater than 13 so swap
17 25 13 31 2 31 is greater than 2 so swap
17 25 13 2 31
40.
Pass 2 :
1725 13 2 31 17 is not greater 25 so no swap
(0) (1) (2) (3) (4)
17 25 13 2 31 25 is greater than 13 so swap
17 13 25 2 31 25 is greater than 2 so swap
17 13 2 25 31
Pass 3 :
17 13 2 25 31 17 is greater than 13 swap
(0) (1) (2) (3) (4)
13 17 2 25 31 17 is greater than 2 swap
13 2 17 25 31
41.
Pass 4 :
132 17 25 31 13 is greater than 2 so swap
(0) (1) (2) (3) (4)
2 13 17 25 31
2 13 17 25 31
Sorted Array is:
2 13 17 25 31
44.
SWAP TWO NUMBERS
•Suppose we have three glass A, B, Temp. Glass
A contain 30, Glass B contain 50 and Glass
Temp is empty now we want to interchange
these two glass (A, B) valuse. So we follow
below steps...
1. First we take Glass A and there values place
inside Glass Temp
2. Now Glass A is empty, take Glass B and
there values place inside Glass A.
3. Now Glass B is empty, take Glass Tempa and
there values place inside Glass B
4. Finally Both Glass values are exchange,
Glass A contain 50 and Glass B contain 30
45.
WRITE C PROGRAMFOR BUBBLE SORT
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[10] ,n, i, j, temp;
clrscr();
printf("Enter Size of Array : ");
scanf("%d",&n);
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for(i = 0; i < n - 1; i++) // to keep track of number of passes
46.
{
for(j = 0;j < n - i - 1; j++) // to compare the elements within the particular
passes
{
// swap if one element is greater than its adjacent element
if(arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("After Bubble Sorting:n");
for(i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
getch();
}
47.
#include <stdio.h>
#include <string.h>// Required for strcmp and strcpy
#include<conio.h>
void main() {
char arr[5][50] = {"Banana", "Apple", "Cherry", "Date", "Grape"};
char temp[50]; // Temporary string for swapping
int i,j;
clrscr();
printf("Original array of strings: ");
for ( i = 0; i < 5; i++) {
printf("%s ", arr[i]);
}
WRITE C PROGRAM FOR BUBBLE SORT STRING ARRAY
48.
for (i =0; i < 5 - 1; i++) {
for ( j = 0; j < 5 - i - 1; j++) {
// Compare adjacent strings
if (strcmp(arr[j], arr[j + 1]) > 0)
{
// Swap them if they are in the wrong order
strcpy(temp, arr[j]);
strcpy(arr[j], arr[j + 1]);
strcpy(arr[j + 1], temp);
}
}
}
printf("nSorted array of strings (ascending): ");
for ( i = 0; i < 5; i++) {
printf("%s ", arr[i]);
}
getch();
}
49.
/* Logic forBubble Sort */
for (i = 0; i < n-1; i++) {
for (j = 0; j < n - i-1; j++) {
if (strcmp(name[j], name[j + 1]) > 0)
{ // Compare adjacent strings
strcpy(temp, name[j]); // Swap strings using temporary variable
strcpy(name[j], name[j + 1]);
strcpy(name[j + 1], temp);
}
}
}
printf("The strings appear after sorting:n"); // Display the sorted strings
for (i = 0; i<=n; i++)
{
printf("%sn", name[i]);
}
getch();
}
50.
#include <stdio.h>
#include <string.h>
#include<conio.h>
voidmain() {
char name[25][50], temp[25]; // Declares an array of strings and a temporary string
int n, i, j; // Declare variables for number of strings and iteration
clrscr();
printf("nnSorts the strings of an array using bubble sort :n"); // Display information about the task
printf("-----------------------------------------------------n");
printf("Input number of strings: ");
scanf("%d", &n); // Read the number of strings from the user
printf("Input string %d :n", n);
for (i = 0; i<=n; i++) {
fgets(name[i],sizeof(name[i]),stdin); // Read strings from the user
}
Write c program for bubble sort on string array
52.
Advantages of BubbleSort
Simplicity: Easy to understand and implement.
Minimal code: Requires minimal lines of code for implementation.
Suitable for small datasets:Works well for small-sized arrays or lists.
Stable: Elements with equal values retain their relative order.
Disadvantages of Bubble Sort
Inefficiency:The time complexity of O(n^2) makes it inefficient for large datasets.
Slow for large arrays: It takes more time to sort than more efficient algorithms.
Not suitable for real-world applications: Other sorting algorithms like Quick Sort or Merge
Sort are preferred for practical use.
Lacks scalability: Does not perform well when dealing with many elements.