• No results found

Data Structures & Algorithms

N/A
N/A
Protected

Academic year: 2021

Share "Data Structures & Algorithms"

Copied!
39
0
0

Loading.... (view fulltext now)

Full text

(1)

Lecture 3 & 4:

Halabja University

Collage of Science /Computer Science Department

2020-2021

Karwan Mahdi

[email protected]

Data Structures & Algorithms

(2)

Introduction to Search Algorithms which is consist of Linear Search and Binary Search

Understand the significance of sorting

Distinguish between internal and external sorting

Understand the logic behind the simple sorting methods of bubble sort, selection sort, and insertion sort.

(3)

Introduction to Search Algorithms

Search: locate an item in a list of information. Two common algorithms (methods): • Linear search

• Binary search

Linear search: also called sequential search. Examines all values in an array until it finds a match or reaches the end.

• Number of visits for a linear search of an array of n elements:The average search visits n/2 elements

The maximum visits is n

A linear search locates a value in an array in O(n) steps

(4)
(5)

Algorithm of Linear Search

(6)

Advantages and Disadvantages

• Benefits:

• Easy algorithm to understand • Array can be in any order • Disadvantages:

• Inefficient (slow): for array of N elements, examines N/2 elements on average for value in array,

• N elements for value not in array

(7)

Binary Search

Binary search is a smart algorithm that is much more efficient than the linear search ,the elements in the array must be sorted in order

1. Divides the array into three sections: • middle element

• elements on one side of the middle element

• elements on the other side of the middle element

2. If the middle element is the correct value, done. Otherwise, go to step 1. using only the half of the array that may contain the correct value.

3. Continue steps 1. and 2. until either the value is found or there are no more elements to examine.

list

low item middle item high item

Is middle item what we are looking for? If not is it more or less than the target item? (Assume lower)

list

(8)
(9)
(10)
(11)
(12)
(13)
(14)

Advantage and Disadvantages

• Benefits:

• Much more efficient than linear search. For array of N elements, performs at most log2N comparisons

• Disadvantages:

• Requires that array elements be sorted

Homework:

Re-write the Binary Search code by using Arrays.binarySearch( )and Recursion

Student Work 1:

try to read about the following Searching algorims

1) Jump Search

(15)

• Sorting is a procedure for organizing a collection of data items in ascending or descending order

• Sequencing of data item depends on data type

• Numerals are arranged according to their magnitude and sign

• Characters are arranged according to the character set used. Character set can be ASCII, EBCDIC or Unicode

Introduction to sorting algorithms

• Business data are often sorted on some primary and one or more secondary keys. • For example, students result might be arranged by institutions, and, within the

institution, by names and grades

• Sorting facilitates the task of searching a large data collection. • It helps organize business information in a systematic way.

• Sorting is frequently used by DBMS to perform vital database operations.

(16)

• Sorting methods are classified into two categories * Internal Sorting

* External Sorting

• If the data collection to be sorted, is kept in the main memory, sorting is referred to as internal sorting

Types of Sorting

• In practice, the large amount of data is often stored on disks, or magnetic tapes • The sorting of data stored on secondary device is referred to as external sorting • In external sorting, block of data are loaded into the main memory. The data is

sorted in the main memory and written back to the secondary memory device.

• Two algorithms considered here: • Bubble sort

(17)

• Bobble sort works by comparing two adjacent data items, and swapping • Array is scanned several times

• Sorting is completed in several passes

• After first pass, the largest element bubbles up (appears) at the end

(18)
(19)

Example2: Bubble Sort

Array num_list contains:

17

23

5

11

compare values 17 and 23 – in correct order, so no exchange

compare values 23 and 5 – not in correct order, so exchange them

compare values 23 and 11 – not in correct order,so exchange them

Concept:

 Compare 1st two elements, If out of order, exchange them to put in order

 Move down one element, compare 2nd and 3rdelements, exchange if necessary.

Continue until end of array.

(20)

After first pass, array num_list contains:

17

5

11

23

compare values 17 and 5 – not in correct order, so exchange them

compare values 17 and 11 – not in correct order, so exchange them

compare values 17 and 23 – in correct order, so no exchange

(21)

After second pass, array numlist3 contains:

5

11

17

23

compare values 5 and 11 – in correct order,so no exchange

compare values 11 and 17 – in correct order, so no exchange

compare values 17 and 23 – in correct order,so no exchange

No exchanges, so

array is in order

(22)
(23)
(24)
(25)
(26)

Bubble Sort Algorithm

(27)

Advantages and Disadvantages

Benefit:

◦ Easy to understand and implement

Disadvantage:

◦ Inefficient: slow for large arrays

(28)

• The Selection sort method works in two phases

• In first phase we find the largest element in the array • The largest element is swapped with the last element • This process is repeated until the array is sorted

Selection Sort

Bubble sort is inefficient for large arrays because items only move by one element at a time. The selection sort performs fewer exchanges because it moves items immediately to their final position in the array.

Concept for sort in ascending order:

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

(29)
(30)
(31)

Example1:Selection Sort

(32)
(33)
(34)

Advantages and Disadvantages

Benefit:

• More efficient than Bubble Sort, since fewer exchanges • Disadvantage:

• May not be as easy as Bubble Sort to understand

(35)

• The insertion sort systematically picks array elements and inserts them into proper positions, Array is divided in two two regions: sorted and un sorted.

• In each pass, an element is picked from the unsorted region and moved into a appropriate position in sorted reign

(36)
(37)
(38)
(39)

Question…?

Thanks 

References

Related documents

THE PROJECT REPORT SUBMITTED I N PARTIAL FULFILMENT FOR THE REQUIREMENTS OF THE MASTER OF SCIENCE DEGREE IN COMMUNITY ECONOMIC DEVELOPMENT (MSc-CED)... And found it to be in

For a high performance HF ground mounted vertical system, the DX Engineering Hustler 4-BTV Package provides 40/20/15/10 meter band coverage.. Hustler 5-BTV Package - High

Are you also searching for Large Scale Solid Phase Synthesis of Peptide Drugs: Use of ...? Get it only at our library now. the postreaction handling of iodine and the cyclized

More than 60 recipes for simple, tasty snacks, including wine pairings for each, plus vivid color photographs demonstrate how easy these delectable dishes are to

However, there are grounds for believing that their use of a regional average as the benchmark when measuring the international income differential and/or the

In our study, 39 indicators provided evidence for 13 criteria on six different themes with weightings as follows: Corporate Policies (10% of the weighting), Labour Issues (20%),

Tüm bunların yanı sıra cümle başında verilen bir boşluk sıfat ya da zarf ile devam ediyorsa cevap “However/No matter how” olabilir. “Once” bağlacı “present” ya

Firstly, as their English language proficiency course ran alongside the courses at the faculty and their ongoing research, the participants became too occupied with their