(Intro Algo)

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

How to design efficient code that works?

< What is Algorithm?


a clearly specified set of simple instructions to be followed to
solve a problem

< ata structures


ethods of organizing data
< !rogram = algorithms + data structures
!robIem AIgorithm Data Structures
input
< !roblem : Sort N numbers in an array
< Solution (algorithm) : use bubble sort technique
< ata structure: array
< Steps :
1. Read into array
2. Scan pairs from left to right, and swap if out of order
3. Repeat scans until no more changes
8 , 6 , 2 , 3 -- > 6 ,8 , 2, 3 -- > 6 , 2, 8 ,3 -- > 6, 2 , 3 ,8
6 ,2 , 3 , 8 -- > 2 ,6 , 3, 8 -- > 2 , 3, 6 ,8 -- > 2, 3 , 6 ,8
Result (Time):
1) Seconds with 10 numbers (Just fine)
2) ays with 1 million numbers (No good)
< !roblem : Sort N numbers in an array
< 2 Solutions (algorithms)
1. Use bubblesort (array structure)
2. Use Heapsort (tree structure)
nput BubbIesort Heapsort
10 2 seconds 5 seconds
500 100 seconds 10 second
500,000 1,000,000
seconds
3000
seconds
trilions 300 years 6 days
Which one is better ?
< Write faster code
< Write code that saves memory
< Always faster code ???
2 situations
N 1) slow code is ok
N Eg: irect deposit of pay cheque
N Time : 1 hour (from time cheque is entered)
N Result: customer just thinks cheque hasn't cleared yet
N Solution: slower code just fine (maybe simple code is used)
N 2) Slow code is not ok
N Eg: customer transfers money from online account
N Time: takes 15 minutes
N Result: customer panics
N Solution : faster code is needed
< An algorithm is correct
f, for every input instance, it halts with the correct output
< ncorrect algorithms
ight not halt at all on some input instances
ight halt with other than the desired answer
nput (N) AIgo 1 (time) AIgo 2 (time)
100 2 seconds 10 seconds
1000 10 seconds 100 seconds
10000 20 seconds 35000 seconds
100000 Error 6 hours
Which one shouId we choose?
< hoose only right and correct algorithms and data
structures in your program!
< Then choose faster and efficient algorithms
< ou can write code!
< f need a brush up
Read online programming notes
Read programming text book
Get a personal tutor
!ractice, practice and practice
< ubblesort
Need three primary parts
1. !ut numbers into array
2. Scan left to right, and swap if out of order
3. Repeat scans until done
< Start with pseudocode
main
{
define array = {1,3,2,7,3,9,0} ; //part 1
while still needs sorting //part 3
{
compare and sort pairs ; //part 2
}
print answer; //extra
}
< Refine the pseudocode
main
{
array = {1,3,2,7,3,9,0} ; // part 1
while not done // part 3
{
for (each element of array)
{
if (next element is smaller)
{
Swap elements;
}
}
}
print answer;
}
//part 2
main()
{
bool Notone = true;
int i , temp;
int ArrayNum[7] = {1,3,2,7,3,9,0};
while(Notone)
{
Notone = false;
for(i = 0; i < 6 ; i++)
{
if (ArrayNum[i+1] < ArrayNum[i])
{
temp = ArrayNum[i+1];
ArrayNum[i+1] = ArrayNum[i];
ArrayNum[i] = temp;
Notone = true;
}
}
}
for(i = 0; i < 6 ; i++)
cout<<ArrayNum[i]<<endl;
}
< Write a complete source code for the bubblesort
algorithm
< Ask a user to enter random numbers.
< The program should be able to arrange the given
numbers in ascending order.

You might also like