0% found this document useful (0 votes)
49 views12 pages

Search and Sort Techniques

The document discusses techniques for designing efficient algorithms, focusing on search and sort algorithms. It provides guidelines for minimizing RAM and CPU usage. Searching an unsorted array sequentially requires N comparisons, while binary search on a sorted array requires fewer comparisons by eliminating half the remaining elements at each step. Common sorting algorithms described are selection sort, bubble sort, and insertion sort.

Uploaded by

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

Search and Sort Techniques

The document discusses techniques for designing efficient algorithms, focusing on search and sort algorithms. It provides guidelines for minimizing RAM and CPU usage. Searching an unsorted array sequentially requires N comparisons, while binary search on a sorted array requires fewer comparisons by eliminating half the remaining elements at each step. Common sorting algorithms described are selection sort, bubble sort, and insertion sort.

Uploaded by

Ferry Kemperman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

SEARCH AND SORT

TECHNIQUES
FERRY KEMPERMAN
NANJING FOREIGN LANGUAGE SCHOOL
APRIL 2019
ALGORITHM DESIGN: EFFICIENCY

• As a software designer you have to design algorithms that meet the following two basic
requirements:
• 1. Works according to specification (correctness)
• 2. Works in the most efficient manner
• Efficiency of an algorithm is comprised of two main factors:
• - Least possible use of resources (CPU/RAM) during execution
• - Fastest possible execution of your algorithm

When you design an algorithm your design should take efficiency into account, but how?
HOW TO DESIGN AN EFFICIENT ALGORITHM?
A FEW GUIDELINES…..

• Minimize RAM usage during execution


• Every variable / data structure used is stored in RAM during execution.
• Limit the number of variables you use in your algorithm.
• Choose the correct datatypes and data structures to store / retrieve values efficiently.
• Use the right scoping of variables. How long will variables exist in RAM during execution?
• Later more on variable scoping.
• Minimize CPU execution time
• Basic idea: the fewer instructions, the better. It is not that simple, though.
• How many assignments, comparisons etcetera are done during execution?
• What kind of expressions are evaluated and how many times?
• Every assignment / comparison is an I/O (read/write) operation to memory. Expensive!
Algorithm’s efficiency……
Fast execution of your algorithm
This is completely dependent on your algorithm design!
How many loops do you use? How many selections?
Is this really necessary? Or can you do with less?

A major impact on algorithm efficiency is the way you implement how:


- You search for elements in an array (or other data structures)
- You sort elements in an array (or other data structures)

Searching and sorting algorithms for arrays are well know design techniques in
Computer Science.
Before introducing them, let’s use a metaphor to understand the importance
of this.
SUPERMARKET METAPHOR

• You go to the supermarket to get 1 can of coke.


• Strategy 1: Start in the first aisle and look at every product of the shelf to see
if it is the can of coke. If not, proceed to second aisle.
• Strategy 2: Randomly walk around in the supermarket and try to find the can
by looking at the shelves as you pass them.
• Strategy 3: Ask clerk for the right aisle with drinks, proceed to this aisle and
scan shelves in this aisle to find a can of coke.
• Which strategy is the best and why? Which one comes second in terms of
efficiency?
• Explain your answer in terms of instructions?
SEARCHING ALGORITHMS: SEQUENTIAL SEARCH
• Suppose you have an array comprised of 5 integers
• MyArray = [5,3,6,9,10]
• You want to find a certain element, say 9, in this array.
• The array is unsorted.
• You can perform a sequential search by comparing each element in the array to the
element you are looking for.
• First element 5=9? No. Second 3=9? No, 6=9? No. 9-9? Yes. Element found at position 4.
• Basic algorithm, but requires N comparisons, where N is upper bound of array.
• Advantage: can be applied to an unsorted array!
• Sorting is expensive!
• Sequential search is also called linear search.
SEARCHING ALGORITHMS: BINARY SEARCH
• Binary search can only be performed on a sorted array
• MyArray = [4,8,12,18,25,30,34]
• To find an element, say 30, in a sorted array we can use a binary search.
• Instead of starting at the first element, we start in the middle of this array:
• Compare your element with the element in the middle, in this case 18.
• If this middle element is smaller, compare to middle element on the right, if bigger compare to
middle element on the left and so on.
• So 18<30, so smaller. Go right. [25,30,34] is the array to the right. Take middle element again. 30=30!
• We do not have to compare 30 to ALL the elements of the array. This is a huge gain in terms of
efficiency!
• Disadvantage: Only sorted arrays can do a binary search, why?
SORTING ALGORITHMS

• Sorting an array can be done in several ways.


• We will discuss three famous sorting algorithms:.
• Selection Sort
• Bubble Sort
• Insertion Sort
SELECTION SORT
Take an unsorted array.
Put marker on last element N.
Search largest element in array.
Put largest element in the last position.
Move marker to N-1.
Search for largest value.
Put largest element in position N-1.
And so on.
BUBBLE SORT
Start with unsorted array, size N
Compare element 1 and 2.
If 1 bigger than 2, flip them.
Compare element 2 and 3.
If 2 bigger than 3, flip them.
The largest element is now in
Position N.
Repeat process for N-1 elements in
Step 2. Elements N and N-1 are now
In right position.
Repeat this in step 3 for N-2 elements.
Etcetera.
INSERTION SORT

Start with unsorted array.


Take second element and
compare with preceding
elements and insert it at the
right place.
Repeat this for third, fourth
element until list is sorted.

You might also like