0% found this document useful (0 votes)
7 views10 pages

Algo 10

Uploaded by

Yousif Hazim
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)
7 views10 pages

Algo 10

Uploaded by

Yousif Hazim
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/ 10

Algorithms

Analysis and Design


from scratch
‫ ﻣﻦ ﺗﺤﺖ‬Algorithms ‫ﲓ‬ ‫ﺗﺤﻠﻴﻞ وﺗ‬

‫أ حمد تم ول ي‬
(program memory structure) Code Segment

void PrintName(String first, String last){


Console.WriteLine(first + " " + last);
Heap
}

Program Memory
PrintName("Ahmed","Metwally");

first, last

Stack
011110101010100000101
001010101101011100110
Code 010101010101011100110

‫أ حمد تم ول ي‬ Algorithms Analysis and Design from scratch ‫ ﻣﻦ ﺗﺤﺖ‬Algorithms ‫ﲓ‬ ‫ﺗﺤﻠﻴﻞ وﺗ‬
(a function that calling herself) Recursion
using System;
class Program {

public static Int64 Factorial(Int64 num){


if(num == 1){ Heap
return 1;
} else {

Program Memory
return num * Factorial(num -1); Factorial: num=1
}
}
Factorial: num=2
public static void Main (string[] args) {
Console.WriteLine( Factorial(4) );
} Factorial: num=3
}

Console.WriteLine( Factorial(4)
24 ); Factorial: num=4
Factorial(4) = 4 * Factorial(4-1)
6 Stack
Factorial(4-1) = 3 * Factorial(3-1)
2
011110101010100000101
001010101101011100110
Factorial(3-1) = 2 * Factorial(2-1)
1 Code 010101010101011100110

Factorial(2-1) = 1

‫أ حمد تم ول ي‬ Algorithms Analysis and Design from scratch ‫ ﻣﻦ ﺗﺤﺖ‬Algorithms ‫ﲓ‬ ‫ﺗﺤﻠﻴﻞ وﺗ‬
Merge Sort

9 5 1 4

9 5 1 4

9 5 1 4

5 9 1 4

1 4 5 9

‫أ حمد تم ول ي‬ Algorithms Analysis and Design from scratch ‫ ﻣﻦ ﺗﺤﺖ‬Algorithms ‫ﲓ‬ ‫ﺗﺤﻠﻴﻞ وﺗ‬
Merge Sort
6 5 12 10 9 1

6 5 12 10 9 1

6 5 12 10 9 1

5 12 9 1

5 12 1 9

5 6 12 1 9 10

6 5 12 10 9 1

‫أ حمد تم ول ي‬ Algorithms Analysis and Design from scratch ‫ ﻣﻦ ﺗﺤﺖ‬Algorithms ‫ﲓ‬ ‫ﺗﺤﻠﻴﻞ وﺗ‬
(How merge done #01) Merge Sort

10 1 9

10 1 9 1

10 1 9 1 9

10 1 9 1 9 10

‫أ حمد تم ول ي‬ Algorithms Analysis and Design from scratch ‫ ﻣﻦ ﺗﺤﺖ‬Algorithms ‫ﲓ‬ ‫ﺗﺤﻠﻴﻞ وﺗ‬
(How merge done #02) Merge Sort

5 6 12 1 9 10

5 6 12 1 9 10 1

5 6 12 1 9 10 1 5

5 6 12 1 9 10 1 5 6

5 6 12 1 9 10 1 5 6 9

5 6 12 1 9 10 1 5 6 9 10

5 6 12 1 9 10 1 5 6 9 10 12

‫أ حمد تم ول ي‬ Algorithms Analysis and Design from scratch ‫ ﻣﻦ ﺗﺤﺖ‬Algorithms ‫ﲓ‬ ‫ﺗﺤﻠﻴﻞ وﺗ‬
(How merge done #03) Merge Sort

8 10 14 1 7

8 10 14 1 7 1

8 10 14 1 7 17 7

8 10 14 1 7 1 7 8 10 14

‫أ حمد تم ول ي‬ Algorithms Analysis and Design from scratch ‫ ﻣﻦ ﺗﺤﺖ‬Algorithms ‫ﲓ‬ ‫ﺗﺤﻠﻴﻞ وﺗ‬
(Algorithm) Merge Sort

1- Read the Array (x), Start and End index of the


portion that we need to sort.
2- Don't continue if End equal or greater than Start.
3- Calculate the Midpoint = (Start + End)/2
4- Divide the portion of the array into NEW two arrays
5- Call yourself twice, one for the left portion, the
other for the right portion
6- merge the two portions.
MERGE-SORT(A, p, r)
1 if p < r
7- print x 2 q = (p + r)/2
3 MERGE-SORT(A, p, q)
4 MERGE-SORT(A, q + 1, r)
5 MERGE(A, p, q, r)

‫أ حمد تم ول ي‬ Algorithms Analysis and Design from scratch ‫ ﻣﻦ ﺗﺤﺖ‬Algorithms ‫ﲓ‬ ‫ﺗﺤﻠﻴﻞ وﺗ‬
(Algorithm #02 merge only) Merge Sort

1- Read the Array (x), Start, midpoint and End index.


2- Create two new arrays for each side.
3- compare all items in the arrays and sort it in the
original array
4- move remain items in each array to the original
array as is

‫أ حمد تم ول ي‬ Algorithms Analysis and Design from scratch ‫ ﻣﻦ ﺗﺤﺖ‬Algorithms ‫ﲓ‬ ‫ﺗﺤﻠﻴﻞ وﺗ‬

You might also like