You are on page 1of 80

1

4 X

LOOK : X

1 n1 = = n-1 Tn= n EFFICIEN O( C(n-1)

1 + +

RESULT:
Proposed

two solution: closed circuit and open circuit close circuit-the path lead to square to the starting square on the 64th move open circuit-the paths ends on any square but not ends near the starting square. Implementation discovered as inefficient but one solution becomes four

Definition :

Nature:

Example :

89 17 17 17 17 17 17

45 45 29 29 29 29

68 68 68 34 34 34 29

90 90 90 90 45 45 34

29 29 45 45 90 68 45

34 34 34 68 68 90 68

17 89 89 89 89 89 89

initial swap 89, 17 swap 45, 29 swap 68, 34 swap 90, 45 swap 90, 68 90 done

Sample Algorithm
for(int i = 1 ; i < n - 1 ; i++) { int k = i; for(int j = i + 1 ; i < n ; i++) { if(A[j] < A[k]) k = j; } if(k!=i) { int temp = A[i]; A[i] = A[k]; A[k] = temp; } }

Computation: TIME COMPLEXITY


for (i=1;i<n;i++){ //n+1 k=i; //1*n //2n+1 for(j=i+1;j<=n;j++){ //(n+1)*n = n2+n if(a[j]<a[k]) //1(n2) k=j; //1(n2) } //3n2+n //3n2+3n+1 if(k!=i){ //1*n temp = a[i]; //1*n a[i]=a[k]; //1*n a[k]=temp; //1*n //4n //3n2+7n+1 } } Total: 3n2+7n+1

Computation: BIG OH

Computation: Omega

F(n) = 3n2+7n+1 C(n) = O( n2 )

Computation: Theta

Formula : O <= c.g(n)<=fn) <=c.g(n) Solution: O <= c.g(n)<=fn)<= c.g(n) O<= n2 <= 3n2+7n+1 <= n2 O<= n2 <=(3n2+7n+1 )/ n2 <= n2 O<= n2 /4 <=(3+7/n + 1/ n2 )<= n2
Substitute:

n=1 O<= c <= (3+7/1 + 1/ 12 ) <= C C<= 11

Computation: Theta
n= 5 O<= c <= 3+7/5+ 1/ 52 <= c O<= c<= ( 15 +7 +25)/25 <= c O<= c <=47/25 <= c C<= 1.88 n = 10 O <= c <= 3+7/10+ 1/ 100<= c O<= c <= ( 300 +70 +4)/100<= c O<= c <= 374/100<= c C<= 3.74

Real world example


Automated election vote ranking Survey Ranking

Exercise: 1 half yellow paper


1. Define the brute force algorithm(2pts) 2. How does the selection sort algorithm work? (3pts.) 3. What is the characteristics of Bubble sorting 4. What is the difference between the selection and bubble sorting algorithm.

BUBBLE SORTING

WHAT IS BUBBLE SORT?

Bubble sort is a simplesorting algorithmthat works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items andswappingthem if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list

CHARACTERISTICS OF BUBBLE SORT


Utilizes Brute Force Algorithm One of the simplest sorting algorithm making it easy to understand even by beginners. One of the most popular algorithm. Though simple, itsO(n2)complexity means it is far too inefficient for use on lists having more than a few elements thus leading controversies regarding its usefulness. As well as being inefficient, it is erroneous that even when all items are already sorted, the algorithm still does not stop. However many modifications have been made in order to solve the problem if ever encountered

ORIGIN OF BUBBLE SORT

ORIGINS OF BUBBLE SORT


In

1956 a reference to analysis of the algorithm regarding sorting called Sorting by Exchange was but does not directly called Bubble Sort. extensive bibliography and sequence of articles from the 1962 ACM Conference on Sorting do not use the term bubble sort, although the Sorting by Exchange algorithm

An

ORIGINS OF BUBBLE SORT

An early 1959 book on

programming devotes a chapter of sorting, but uses the term Exchange Sort rather than bubble sort.

ORIGINS OF BUBBLE SORT


In

1962, Iverson uses the name Bubble Sort. This appears to be the first use of the term in print. Thus reinforces claims that Iverson is the first to use the term, though not conclusive. earlier publications the algorithm officially enters the ACM algorithm repository as Algorithm 175 in 1963 where it is named Shuttle Sort.

Despite

ALGORITHM SIMULATION

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
old [5 new [5 1 1 array 4 4 2 2 8] 8]

n=5 x=4 y=0 array[y] = 5 array[y+1] = 1 temp = null

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[5 [5 1 1 array 4 4 2 2 8] 8]

n=5 x=4 y=0 array[y] = 5 array[y+1] = 1 temp = null

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[5 [5 1 1 array 4 4 2 2 8] 8]

n=5 x=4 y=0 array[y] = 5 array[y+1] = 1 temp = null

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[5 [5 1 1 array 4 4 2 2 8] 8]

n=5 x=4 y=0 array[y] = 5 array[y+1] = 1 temp = null

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[(5 [(1 1) 5) array 4 4 2 2 8] 8]

n=5 x=4 y=1 array[y] = 1 array[y+1] = 5 temp = 5

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 5 5 array 4 4 2 2 8] 8]

n=5 x=4 y=1 array[y] = 5 array[y+1] = 4 temp = 5

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 5 5 array 4 4 2 2 8] 8]

n=5 x=4 y=1 array[y] = 5 array[y+1] = 4 temp = 5

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 (5 (4 array 4) 5) 2 2 8] 8]

n=5 x=4 y=2 array[y] = 4 array[y+1] = 5 temp = 5

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 4 4 array 5 5 2 2 8] 8]

n=5 x=4 y=2 array[y] = 5 array[y+1] = 2 temp = 5

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 4 4 array 5 5 2 2 8] 8]

n=5 x=4 y=2 array[y] = 5 array[y+1] = 2 temp = 5

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 4 4 array (5 (2 2) 5) 8] 8]

n=5 x=4 y=3 array[y] = 2 array[y+1] = 5 temp = 5

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 4 4 array 2 2 5 5 8] 8]

n=5 x=4 y=3 array[y] = 5 array[y+1] = 8 temp = 5

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 4 4 array 2 2 5 5 8] 8]

n=5 x=4 y=4 array[y] = 5 array[y+1] = 8 temp = 5

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 4 4 array 2 2 5 5 8] 8]

n=5 x=(4-1) = 3 y=4 array[y] = 5 array[y+1] = 8 temp = 5

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 4 4 array 2 2 5 5 8] 8]

n=5 x=3 y=4 array[y] = 5 array[y+1] = 8 temp = 5

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 4 4 array 2 2 5 5 8] 8]

n=5 x=3 y=0 array[y] = 0 array[y+1] = 4 temp = 5

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 4 4 array 2 2 5 5 8] 8]

n=5 x=3 y=1 array[y] = 0 array[y+1] = 4 temp = 5

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 4 4 array 2 2 5 5 8] 8]

n=5 x=3 y=1 array[y] = 4 array[y+1] = 2 temp = 5

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 (4 (2 array 2) 4) 5 5 8] 8]

n=5 x=3 y=2 array[y] = 2 array[y+1] = 4 temp = 4

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 2 2 array 4 4 5 5 8] 8]

n=5 x=3 y=2 array[y] = 4 array[y+1] = 5 temp = 4

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 2 2 array 4 4 5 5 8] 8]

n=5 x=3 y=3 array[y] = 4 array[y+1] = 5 temp = 4

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 2 2 array 4 4 5 5 8] 8]

n=5 x=(3-1)=2 y=3 array[y] = 4 array[y+1] = 5 temp = 4

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 2 2 array 4 4 5 5 8] 8]

n=5 x=2 y=3 array[y] = 4 array[y+1] = 5 temp = 4

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 2 2 array 4 4 5 5 8] 8]

n=5 x=2 y=0 array[y] = 1 array[y+1] = 2 temp = 4

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 2 2 array 4 4 5 5 8] 8]

n=5 x=2 y=1 array[y] = 1 array[y+1] = 2 temp = 4

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 2 2 array 4 4 5 5 8] 8]

n=5 x=2 y=1 array[y] = 2 array[y+1] = 4 temp = 4

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 2 2 array 4 4 5 5 8] 8]

n=5 x=2 y=2 array[y] = 2 array[y+1] = 4 temp = 4

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 2 2 array 4 4 5 5 8] 8]

n=5 x=(2-1) = 1 y=2 array[y] = 4 array[y+1] = 5 temp = 4

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 2 2 array 4 4 5 5 8] 8]

n=5 x=1 y=2 array[y] = 4 array[y+1] = 5 temp = 4

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 2 2 array 4 4 5 5 8] 8]

n=5 x=1 y=0 array[y] = 1 array[y+1] = 2 temp = 4

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 2 2 array 4 4 5 5 8] 8]

n=5 x=1 y=1 array[y] = 1 array[y+1] = 2 temp = 4

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 2 2 array 4 4 5 5 8] 8]

n=5 x=(1-1) = 0 y=1 array[y] = 1 array[y+1] = 2 temp = 4

ALGORITHM SIMULATION
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } }
[1 [1 2 2 array 4 4 5 5 8] 8]

n=5 x=0 y=1 array[y] = 1 array[y+1] = 2 temp = 4

MORE EFFICIENT CODE


do { swap = false; for(int index = 0; index < SIZE; index++) { try{ if(array[index] > array[index+1]) { temp = array[index]; array[index] = array[index+1]; array[index+1] = temp; swap = true;} } catch(ArrayIndexOutOfBoundsException e){ temp = 0; } } }while(swap); //Program Courtesy of Mark Alvarez

TIME COMPLEXITY
for(x=n-1;x>0;x--){ for(y=0;y<x;y++){ if(array[y]>array[y+1]){ temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; } } N+1 N+1(N) 1(N2) 1(N2) 1(N2) 1(N2) Total: 5N2+2n+1

RESEARCHES THAT USES BUBBLE SORT

POLYGON FILL ALGORITHM

POLYGON FILL ALGORITHM

While the point-in-polygon algorithm is useful for determining whether a few points are inside a polygon, it is woefully inefficient for filling a polygon, because it requires checking every side of the polygon for every pixel in the image. To speed things up tremendously, we will check each side of the polygon only once per pixel row. It works like this:

POLYGON FILL ALGORITHM

POLYGON FILL ALGORITHM

Figure 1 shows a polygon. We are about to render one row of pixels. All the pixels on that row have the same Y coordinate, which is represented by the red line in the figure. Loop through the polygon and build a list of threshold-crossing nodes, just as in the pointin-polygon algorithm but instead of comparing them with an X coordinate, store them all in a list. Figure 1 shows the indices (0 through 5) of the nodes. In this example, the polygon starts at the blue corner, and is traced counter-clockwise, which generates a fairly random horizontal order for the nodes.

POLYGON FILL ALGORITHM

Next, sort the node list so that it proceeds from left-to-right, as shown in Figure 2. This takes a little time, but we have to do it onlyonceper pixel row.

POLYGON FILL ALGORITHM

POLYGON FILL ALGORITHM

Now, as shown in Figure 3, it is a simple matter to fill all the pixels between each pair of nodes from node 0 to 1, node 2 to 3, and node 4 to 5.

POLYGON FILL ALGORITHM

Unfortunately because the Bubble Sorting Algorithm is so inefficient that most programmers and designers questions the usefulness automatically disregards it to a more faster and efficient algorithm such as quick sort, shell sort, etc. However, due to its simplicity, most books and sources that teaches data structures and algorithm still includes Bubble Sort as one of the best example of a sorting algorithm for it is easy to explain and understand for beginners.

You might also like