View by Category

The presentation will start after a short

(15 second) video ad from one of our sponsors.

Hot tip: Video ads won’t appear to registered users who are logged in. And it’s free to register and free to log in!

(15 second) video ad from one of our sponsors.

Hot tip: Video ads won’t appear to registered users who are logged in. And it’s free to register and free to log in!

Loading...

PPT – ICS103 Programming in C Lecture 14: Searching and Sorting PowerPoint presentation | free to view - id: 158a13-ZjNmN

The Adobe Flash plugin is needed to view this content

About This Presentation

Write a Comment

User Comments (0)

Transcript and Presenter's Notes

ICS103 Programming in CLecture 14 Searching

and Sorting

Outline

- Searching
- Linear Search Algorithm
- Linear Search Implementation
- Binary Search Algorithm
- Binary Search Implementation
- Sorting
- Selection Sort Algorithm
- Selection Sort Implementation
- Bubble Sort Algorithm
- Bubble Sort Implementation

Introduction to Searching

- Searching means scanning through a list of items

(in an array) to find if a particular one exists. - It usually requires the user to specify the

target item the item he wishes to locate - If the target item is found, the item or its

location (index) is returned, otherwise, an

appropriate message or flag is returned. - An important issue in processing a search request

is response time. Some factors affecting

response time are - The size of the array to search from
- The organization of data in the array random or

ordered - The searching method or algorithm linear or

binary - In this lecture, we study two searching methods

Linear Search and Binary Search.

Linear Search Algorithm

- This involves searching through the array

sequentially until the target item is found or

the array is exhausted. - If the target is found, its location is returned,

otherwise a flag such as 1 is returned. Here is

the algorithm for Linear Search - Assume that the target has not been found
- Start with initial array element
- Repeat while the target is not found and there

are more array elements - If the current element matches the target
- Set a flag to indicate that the target has been

found - else
- Advance to the next array element
- If the target was found
- Return the target index as the search result
- else
- Return -1 as the search result

Linear Search Implementation

- include ltstdio.hgt
- define SIZE 8
- int linear_search(double a, double target, int

size) - void read_array(double a, int size)
- int main(void)
- double xSIZE, target
- int index
- read_array(x, SIZE)
- printf("Enter Element to search for ")
- scanf("lf", target)
- index linear_search(x, target, SIZE)
- if (index ! -1)
- printf("Target was found at index d\n",

index) - else
- printf("Sorry, target item was not

found") - system("pause")

/ Searches for target in an array using Linear

search Returns index of target or -1 if not

found / int linear_search(double a, double

target, int size)

int i, found 0, where i

0 while (!found i lt size)

if (ai target) found 1

else i

if (found) where i else

where -1 return where

Binary Search Algorithm

- For a list of n elements, the linear search takes

an average of n/2 comparisons to find an item,

with the best case being 1 comparison and the

worst case being n comparisons. - However, if the list is ordered, it is a waste of

time to look for an item using linear search - it

would be like looking for a word in a dictionary

sequentially. - In this case we apply a more efficient method

called binary search. Binary search works by

comparing the target with the item at the middle

of the list. This leads to one of three results - If the middle item is the target we are done.
- If the middle item is less than target, we apply

the algorithm to the upper half of the list. - If the middle item is bigger than the target, we

apply the algorithm to the lower half of the

list. - This process is repeated until the item is found

or the list is exhausted

Binary Search Algorithm .

Binary Search Implementation

- include ltstdio.hgt
- define SIZE 8
- int binary_search (double x, int low, int high,

double target) - void read_array(double a, int size)
- int main(void)
- double xSIZE, target
- int index
- read_array(x, SIZE)
- printf("Enter Element to search for ")
- scanf("lf", target)
- index binary_search(x, 0, SIZE-1,

target) - if (index ! -1)
- printf("Target was found at index d\n",

index) - else
- printf("Sorry, target item was not

found") - system("pause")

/ Recursive implementation of binary search

/ int binary_search (double x, int low, int

high, double target) int middle

if (low gt high) /base case1target not

found/ return -1

middle (low high)/2 if (xmiddle

target) return (middle) /base

case2target

found/ else if (xmiddle lt target)

return binary_search(x,

middle1,high,target)

else return binary_search(x,

low,

middle-1,target)

Introduction to Sorting

- Sorting is the re-arrangement of a collection of

data according to some key-field. - It is a common activity in data management. Even

when a list is maintained in a certain order,

there is often a need to re-arrange the list in a

different order. - Because it takes so much processing time, sorting

is a serious topic in computer science, and many

different sorting algorithms have been designed. - We shall consider two of such sorting methods

Selection sort and Bubble Sort.

Selection Sort Algorithm

- Selection sort involved scanning through the list

to find (or select) the smallest element and swap

it with the first element. - The rest of the list is then search for the next

smallest and swap it with the second element. - This process is repeated until the rest of the

list reduces to one element, by which time the

list is sorted. - The following table shows how selection sort

works.

Selection Sort Implementation

- include ltstdio.hgt
- define SIZE 10
- void selection_sort(double a, int size)
- void read_array(double a, int size)
- void print_array(double a, int size)
- int find_min(double a, int start, int size)
- void swap(double a, double b)
- int main(void)
- double xSIZE
- int i
- read_array(x, SIZE)
- printf("Before Sorting ")
- print_array(x, SIZE)
- selection_sort(x, SIZE)
- printf("After Sorting ")
- print_array(x, SIZE)

int find_min(double a, int start, int size)

int i, min_index start for

(istart1 iltsize i) if (ai lt

amin_index) min_index i

return min_index void swap(double a,

double b) double temp a a

b b temp void read_array (double

a, int size) int i printf("Enter

d integer numbers separated by blanks\ngt ",

size) for (i 0 i lt size i)

scanf("lf", ai) void print_array(double

a, int size) int i for

(i 0 i lt size i)

printf(".1f ", ai) printf("\n")

Bubble Sort Algorithm

- The idea of Bubble (or exchange) sort is to scan

through the list and swap each pair of adjacent

elements that are in the wrong order. - The process is repeated each time from index zero

to one less than the previous limit until either

the list is exhausted or until a pass that

involve no swap is encountered. - At the end of first pass, the largest element

will move (or bubble up) to the end of the list.

- At the end of the second swap, the second largest

will move to its right place, etc. - The following table shows a trace of how bubble

sort works.

Bubble Sort Implementation

- include ltstdio.hgt
- define SIZE 10
- void bubble_sort(double a, int size)
- void read_array(double a, int size)
- void print_array(double a, int size)
- void swap(double a, double b)
- int main(void)
- double xSIZE
- int i
- read_array(x, SIZE)
- printf("Before Sorting ")
- print_array(x, SIZE)
- bubble_sort(x, SIZE)
- printf("After Sorting ")
- print_array(x, SIZE)

void bubble_sort(double a, int size) int

i, pass 1, swap_occurs do

swap_occurs 0 for(i 1 i lt size

- pass i) if (ai - 1 gt ai)

swap(ai-1, ai)

swap_occurs 1

pass while (swap_occurs pass lt

size-1) void read_array (double a, int size)

int i printf("Enter d integer

numbers separated by blanks\ngt ", size) for

(i 0 i lt size i) scanf("lf",

ai) void print_array(double a, int size)

int i for (i 0 i lt

size i) printf(".1f ",

ai) printf("\n")

About PowerShow.com

PowerShow.com is a leading presentation/slideshow sharing website. Whether your application is business, how-to, education, medicine, school, church, sales, marketing, online training or just for fun, PowerShow.com is a great resource. And, best of all, most of its cool features are free and easy to use.

You can use PowerShow.com to find and download example online PowerPoint ppt presentations on just about any topic you can imagine so you can learn how to improve your own slides and presentations for free. Or use it to find and download high-quality how-to PowerPoint ppt presentations with illustrated or animated slides that will teach you how to do something new, also for free. Or use it to upload your own PowerPoint slides so you can share them with your teachers, class, students, bosses, employees, customers, potential investors or the world. Or use it to create really cool photo slideshows - with 2D and 3D transitions, animation, and your choice of music - that you can share with your Facebook friends or Google+ circles. That's all free as well!

For a small fee you can get the industry's best online privacy or publicly promote your presentations and slide shows with top rankings. But aside from that it's free. We'll even convert your presentations and slide shows into the universal Flash format with all their original multimedia glory, including animation, 2D and 3D transition effects, embedded music or other audio, or even video embedded in slides. All for free. Most of the presentations and slideshows on PowerShow.com are free to view, many are even free to download. (You can choose whether to allow people to download your original PowerPoint presentations and photo slideshows for a fee or free or not at all.) Check out PowerShow.com today - for FREE. There is truly something for everyone!

You can use PowerShow.com to find and download example online PowerPoint ppt presentations on just about any topic you can imagine so you can learn how to improve your own slides and presentations for free. Or use it to find and download high-quality how-to PowerPoint ppt presentations with illustrated or animated slides that will teach you how to do something new, also for free. Or use it to upload your own PowerPoint slides so you can share them with your teachers, class, students, bosses, employees, customers, potential investors or the world. Or use it to create really cool photo slideshows - with 2D and 3D transitions, animation, and your choice of music - that you can share with your Facebook friends or Google+ circles. That's all free as well!

For a small fee you can get the industry's best online privacy or publicly promote your presentations and slide shows with top rankings. But aside from that it's free. We'll even convert your presentations and slide shows into the universal Flash format with all their original multimedia glory, including animation, 2D and 3D transition effects, embedded music or other audio, or even video embedded in slides. All for free. Most of the presentations and slideshows on PowerShow.com are free to view, many are even free to download. (You can choose whether to allow people to download your original PowerPoint presentations and photo slideshows for a fee or free or not at all.) Check out PowerShow.com today - for FREE. There is truly something for everyone!

presentations for free. Or use it to find and download high-quality how-to PowerPoint ppt presentations with illustrated or animated slides that will teach you how to do something new, also for free. Or use it to upload your own PowerPoint slides so you can share them with your teachers, class, students, bosses, employees, customers, potential investors or the world. Or use it to create really cool photo slideshows - with 2D and 3D transitions, animation, and your choice of music - that you can share with your Facebook friends or Google+ circles. That's all free as well!

For a small fee you can get the industry's best online privacy or publicly promote your presentations and slide shows with top rankings. But aside from that it's free. We'll even convert your presentations and slide shows into the universal Flash format with all their original multimedia glory, including animation, 2D and 3D transition effects, embedded music or other audio, or even video embedded in slides. All for free. Most of the presentations and slideshows on PowerShow.com are free to view, many are even free to download. (You can choose whether to allow people to download your original PowerPoint presentations and photo slideshows for a fee or free or not at all.) Check out PowerShow.com today - for FREE. There is truly something for everyone!

For a small fee you can get the industry's best online privacy or publicly promote your presentations and slide shows with top rankings. But aside from that it's free. We'll even convert your presentations and slide shows into the universal Flash format with all their original multimedia glory, including animation, 2D and 3D transition effects, embedded music or other audio, or even video embedded in slides. All for free. Most of the presentations and slideshows on PowerShow.com are free to view, many are even free to download. (You can choose whether to allow people to download your original PowerPoint presentations and photo slideshows for a fee or free or not at all.) Check out PowerShow.com today - for FREE. There is truly something for everyone!

Recommended

«

/ »

Page of

«

/ »

Promoted Presentations

Related Presentations

Page of

Page of

CrystalGraphics Sales Tel: (800) 394-0700 x 1 or Send an email

Home About Us Terms and Conditions Privacy Policy Contact Us Send Us Feedback

Copyright 2016 CrystalGraphics, Inc. — All rights Reserved. PowerShow.com is a trademark of CrystalGraphics, Inc.

Copyright 2016 CrystalGraphics, Inc. — All rights Reserved. PowerShow.com is a trademark of CrystalGraphics, Inc.

The PowerPoint PPT presentation: "ICS103 Programming in C Lecture 14: Searching and Sorting" is the property of its rightful owner.

Do you have PowerPoint slides to share? If so, share your PPT presentation slides online with PowerShow.com. It's FREE!

Committed to assisting Kfupm University and other schools with their online training by sharing educational presentations for free