0% found this document useful (0 votes)
64 views32 pages

Arrays: Searching and Sorting

This document discusses different searching and sorting algorithms for arrays. It begins by explaining binary search, which searches an ordered array by eliminating half of the remaining elements at each step. It provides pseudocode and an example. Linear search for both ordered and unordered arrays is also covered. The document then discusses bubble sort and selection sort algorithms. Bubble sort works by comparing adjacent elements and swapping them if out of order, while selection sort finds the minimum element and swaps it into the proper place each iteration. Pseudocode for both algorithms and examples are provided.

Uploaded by

JhonSnow
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views32 pages

Arrays: Searching and Sorting

This document discusses different searching and sorting algorithms for arrays. It begins by explaining binary search, which searches an ordered array by eliminating half of the remaining elements at each step. It provides pseudocode and an example. Linear search for both ordered and unordered arrays is also covered. The document then discusses bubble sort and selection sort algorithms. Bubble sort works by comparing adjacent elements and swapping them if out of order, while selection sort finds the minimum element and swaps it into the proper place each iteration. Pseudocode for both algorithms and examples are provided.

Uploaded by

JhonSnow
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

ARRAYS

Searching and sorting

Code : blocks
ICP PRESENTATION
GROUP 7

Group Members
• Umar Masood(FA18-BEE-070)
• Abdul Wahab(FA18-BEE-035)
• Mirza Hamza(FA18-BEE-013)
• Khawar Bodla(FA18-BEE-096)
BINARY SEARCH
• .
• Search an ordered array of integers for a value and return its index if the
value is found, otherwise return -1.

A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7]


1 2 3 4 5 6 7 8

Binary search skips over part of the array if the search value cannot possibly be
there.
BINARY SEARCH
• .
• Binary search is based on the ‘divide and conquer’ strategy which works as
follow:
 Start by looking at the idle element of the array.
1. If the value its hold is lower than the search element, eliminate the first half of the
array from further consideration.
2. If the value it holds is higher than the search element, estimate the th second half
of the array for further consideration.
 Repeat this process until the element is found or until the entire array has
been eliminated.
BINARY SEARCH

• Algorithim:
Set first and last boundary of array to be searched.
Repeat the following:
Find middle element between first and last boundaries.
if(middle element contain the search value)
return middle element position;
else if(first>=last)
return -1;
else if(value<the value of middle element)
set last to middle element -1
BINARY SEARCH
//searches an ordered array of integers
int branch(int data[],//input : array
int size; //input :; array size
int value //input : value to find
) //output: if found return index otherwise return -1
{ int first, middle,last;
first=0; last=size-1;
while(true){ middle=(first+last)/2;
If(data[middle]==value)
return middle;
else if(first>=last)
return -1;else if(value<data[middle])
last=middle-1;else
first=middle+1;}}
EXAMPLE BINARY SEARCH
.
•14?
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7]
1 2 3 5 7 10 14 17

A[4] A[5] A[6] A[7]

7 10 14 17

IN THIS CASE
A[6] A[7]
(data[middle]==value)
Return 0; 14 17
EXAMPLE BINARY SEARCH
•4?.
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7]

1 2 3 5 7 10 14 17

A[0] A[1]
A[2]
1 2 3

In this case (first==last) A[2]


Return -1;
3
LINEAR SEARCHING
• The process used to find the location of a target
• among a list of objects
• Searching an array finds the index of first element in
• an array containing that value
UNORDERED LINEAR SEARCH
• Search an unordered array of integers for a value and return its
index if the value is found. Otherwise, return -1.

• A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7]


14 3 25 10 6 9 13 21

• Algorithm:
Start with the first array element (index 0)
while(more elements in array){
if value found at current index, return index;
Try next element (increment index);
}
Value not found, return -1;
UNORDERED LINEAR SEARCH
// Searches an unordered array of integers
int search(int data[], //input: array
int size, //input: array size
int value){ //input: search value
// output: if found, return index;
// otherwise, return –1.
for(int index = 0; index < size; index++){
if(data[index] == value)
return index;
}
return -1;
}
ORDERED LINEAR SEARCH
• Search an ordered array of integers for a value
and return its index if the value is found;
Otherwise, return -1.

• A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7]


1 3 5 7 11 14 15 17
• Linear search can stop immediately when it
has passed the possible position of the search
value.
ORDERED LINEAR SEARCH
• Algorithm:
Start with the first array element (index
0)
while(more elements in the array){
if value at current index is greater
than value,
value not found, return
–1;
if value found at current index,
return index;
Try next element (increment index);
}
value not found, return –1;
ORDERED LINEAR SEARCH
// Searches an ordered array of integers
int lsearch(int data[],// input: array
int size, // input: array size
int value // input: value to find
) { // output: index if found
for(int index=0; index<size; index++){
if(data[index] > value)
return -1;
else if(data[index] == value)
return index;
}
return -1;
}
INTRO TO SORTING ARRAYS
• Sort: arrange values into an order:
• Alphabetical
• Ascending numeric
• Descending numeric
• Two algorithms considered here:
• Bubble sort
• Selection sort
BUBBLE SORT
Concept:
• Compare 1st two elements
• If out of order, exchange them to put in order
• Move down one element, compare 2nd and 3rd elements, exchange if
necessary. Continue until end of array.
• Pass through array again, exchanging as necessary
• Repeat until pass made with no exchanges
BUBBLE SORT EXAMPLE-1

17 23 5 11

23 and 11 are also in wrong order so we swap


17 and 23 are in correct order so we donot change them
them

23 and 5 are in incorrect order so we swap them


BUBBLE SORT EXAMPLE-2
.

17 5 11 23

17 and 23 are in correct order so we let them be


17 and 5 are in incorrect order so we change
them

17 and 11 are in incorrect order so we swap them


BUBBLE SORT EXAMPLE-3
•.

5 11 17 23

17 and 123 are also in correct order so we


5 and 11 are in correct order so donot swap them donot swap them

11 and 17 are in correct order so we donot swap them


PROGRAM IN -C
• #include <stdio.h> • if (array[d] > array[d+1])
• int main() • {
• swap = array[d];
• { • array[d] = array[d+1];
• int array[100], n, c, d, swap; • array[d+1] = swap;
• }
• printf("Enter number of elements\n"); • }
• scanf("%d", &n); • }

• for (c = 0; c < n; c++) • printf("Sorted list in ascending order:\n");


• scanf("%d", &array[c]);
• for (c = 0; c < n; c++)
• for (c = 0 ; c < n - 1; c++) • printf("%d\n", array[c]);
• {
• return 0;
• for (d = 0 ; d < n - c - 1; d++) • }
• {
BUBBLE SORT- TRADEOFFS
• Benefit:
• Easy to understand and implement

• Disadvantage:
• Inefficient: slow for large arrays
SELECTION SORT

Concept for sort in ascending order:


• Locate smallest element in array. Exchange it with element in position 0

• Locate next smallest element in array. Exchange it with element in position 1.

• Continue until all elements are arranged in order


SELECTION SORT-EXAMPLE
• Array numlist contains:

11 2 29 3
1. Smallest element is 2. Exchange 2 with element in 1st position in array

2 11 29 3
SELECTION SORT-EXAMPLE
2. Next smallest element is 3. Exchange 3 with element in 2nd position in
array:
2 3 29 11
3. Next smallest element is 11. Exchange 11 with element in 3rd position in
array

2 3 11 29
PROGRAM-PART:1
• #include <stdio.h>
n=>number of array elements
• int main() c=>counter used in 1st loop
• { d=>counter used in 2nd loop
• int array[100], n, c, d, position, swap; position=>to locate position of smallest element

• printf("Enter number of elements\n");


• scanf("%d", &n);

• printf("Enter %d integers\n", n); Entering array elements

• for (c = 0; c < n; c++)


• scanf("%d", &array[c]);
PROGRAM- PART:2
• for (c = 0; c < (n - 1); c++)
• {
• position = c;
• for (d = c + 1; d < n; d++)
• {
• if (array[position] > array[d]) Locating smallest element in array
• position = d;
• }
• if (position != c)
• { If not in c(index) then swap with
• swap = array[c]; element present in position(index)
• array[c] = array[position]; which contain smallest element
• array[position] = swap; index
• }
• }
SELECTION SORT TRADEOFFS
• Benefit:
• More efficient than Bubble Sort, since fewer exchanges

• Disadvantage:
• May not be as easy as Bubble Sort to understand

You might also like