Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ambily Krishnan PGT CS KV, Thrissur

Similar presentations


Presentation on theme: "Ambily Krishnan PGT CS KV, Thrissur"— Presentation transcript:

1 Ambily Krishnan PGT CS KV, Thrissur
Arrays : Bubble Sort Ambily Krishnan PGT CS KV, Thrissur

2 Before & After Iteration : 1
BUBBLE SORT I=0 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 13 J=0 1 19 2 6 J+1=1 3 2 4 35 5 28 Condition false . No change Temp = 6 51 7 16 8 65 9 4 N = 10 I < N (9) J < (N-1)-I (9)

3 Before Iteration = 2 BUBBLE SORT I=0 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 13 J=1 1 19 2 6 J+1=2 3 2 4 35 5 28 Condition true . Temp = 19 Ar[1] = 6 Ar[2] = 19 6 51 7 16 8 65 9 4 N = 10 I < N (9) J < (N-1)-I (9)

4 After Iteration = 2 BUBBLE SORT I=0 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 13 J=1 1 6 2 19 J+1=2 3 2 4 35 5 28 Condition true . Temp = 19 Ar[1] = 6 Ar[2] = 19 6 51 7 16 8 65 9 4 N = 10 I < N (9) J < (N-1)-I (9)

5 Before Iteration = 3 BUBBLE SORT I=0 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 13 J=2 1 6 2 19 J+1=3 3 2 4 35 5 28 Condition true Temp = 19 Ar[2]=2 Ar[3]=19 6 51 7 16 8 65 9 4 N = 10 I < N (9) J < (N-1)-I (9)

6 After Iteration = 3 BUBBLE SORT I=0 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 13 J=2 1 6 2 2 J+1=3 3 19 4 35 5 28 Condition true Temp = 19 Ar[2]=2 Ar[3]=19 6 51 7 16 8 65 9 4 N = 10 I < N (9) J < (N-1)-I (9)

7 Before & After Iteration = 4
BUBBLE SORT I=0 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 13 J=3 1 6 2 2 J+1=4 3 19 4 35 5 28 Condition false No Change 6 51 7 16 8 65 9 4 N = 10 I < N (9) J < (N-1)-I (9)

8 Before Iteration = 5 BUBBLE SORT I=0 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 13 J=4 1 6 2 2 J+1=5 3 19 4 35 5 28 Condition true Inter Change value 6 51 7 16 8 65 9 4 N = 10 I < N (9) J < (N-1)-I (9)

9 After Iteration = 5 BUBBLE SORT I=0 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 13 J=4 1 6 2 2 J+1=5 3 19 4 28 5 35 Condition true Inter Change value 6 51 7 16 8 65 9 4 N = 10 I < N (9) J < (N-1)-1 (9)

10 Before & After Iteration = 6
BUBBLE SORT I=0 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-<;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 13 J=5 1 6 2 2 J+1=6 3 19 4 28 5 35 Condition false No Change 6 51 7 16 8 65 9 4 N = 10 I < N (9) J < (N-1)-1 (9)

11 Before Iteration = 7 BUBBLE SORT I=0 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 13 J=6 1 6 2 2 J+1=7 3 19 4 28 5 35 Condition true inter Change value 6 51 7 16 8 65 9 4 N = 10 I < N (9) J < (N-1)-I (9)

12 After Iteration = 7 BUBBLE SORT I=0 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 13 J=6 1 6 2 2 J+1=7 3 19 4 28 5 35 Condition true inter Change value 6 16 7 51 8 65 9 4 N = 10 I < N (9) J < (N-1)-I (9)

13 Before & After Iteration = 8
BUBBLE SORT I=0 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 13 J=7 1 6 2 2 J+1=8 3 19 4 28 5 35 Condition False No Change 6 16 7 51 8 65 9 4 N = 10 I < N (9) J < (N-1)-I (9)

14 Before Iteration = 9 BUBBLE SORT I=0 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 13 J=8 1 6 2 2 J+1=9 3 19 4 28 5 35 Condition true Inter Change Value 6 16 7 51 8 65 9 4 N = 10 I < N (9) J < (N-1)-I (9)

15 Before Iteration = 9 BUBBLE SORT I=0 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 13 J=8 1 6 2 2 J+1=9 3 19 4 28 5 35 Condition true Inter Change 6 16 7 51 8 4 9 65 N = 10 I < N (9) J < (N-1)-I (9)

16 Iteration = 10(inner loop exit)
BUBBLE SORT I=0 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 13 J<9 condition false J=9 1 6 2 2 J+1=9 3 19 4 28 5 35 6 16 7 51 8 4 9 65 N = 10 I < N (9) J < (N-1)-I (9)

17 Before Iteration = 1 BUBBLE SORT I=1 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 13 J=0 1 6 2 2 J+1=1 3 19 4 28 5 35 6 16 7 51 8 4 9 65 N = 10 I < N (9) J < (N-1)-I (8)

18 After Iteration = 1 BUBBLE SORT I=1 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 6 J=0 1 13 2 2 J+1=1 3 19 4 28 5 35 6 16 7 51 8 4 9 65 N = 10 I < N (9) J < (N-1)-I (8)

19 Before Iteration = 2 BUBBLE SORT I=1 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 6 J=1 1 13 2 2 J+1=2 3 19 4 28 5 35 6 16 7 51 8 4 9 65 N = 10 I < N (9) J < (N-1)-I (8)

20 After Iteration = 2 BUBBLE SORT I=1 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 6 J=1 1 2 2 13 J+1=2 3 19 4 28 5 35 6 16 7 51 8 4 9 65 N = 10 I < N (9) J < (N-1)-I (8)

21 Before & After Iteration = 3
BUBBLE SORT I=1 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 6 J=2 1 2 2 13 J+1=3 3 19 4 28 5 35 6 16 7 51 8 4 9 65 N = 10 I < N (9) J < (N-1)-I (8)

22 Before & After Iteration = 4
BUBBLE SORT I=1 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 6 J=3 1 2 2 13 J+1=4 3 19 4 28 5 35 6 16 7 51 8 4 9 65 N = 10 I < N (9) J < (N-1)-I (8)

23 Before & After Iteration = 5
BUBBLE SORT I=1 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 6 J=4 1 2 2 13 J+1=5 3 19 4 28 5 35 6 16 7 51 8 4 9 65 N = 10 I < N (9) J < (N-1)-I (8)

24 Before Iteration = 6 BUBBLE SORT I=1 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 6 J=5 1 2 2 13 J+1=6 3 19 4 28 5 35 6 16 7 51 8 4 9 65 N = 10 I < N (9) J < (N-1)-I (8)

25 After Iteration = 6 BUBBLE SORT I=1 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 6 J=5 1 2 2 13 J+1=6 3 19 4 28 5 16 6 35 7 51 8 4 9 65 N = 10 I < N (9) J < (N-1)-I (8)

26 Before & After Iteration = 7
BUBBLE SORT I=1 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 6 J=6 1 2 2 13 J+1=7 3 19 4 28 5 16 6 35 7 51 8 4 9 65 N = 10 I < N (9) J < (N-1)-I (8)

27 Before Iteration = 8 BUBBLE SORT I=1 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 6 J=7 1 2 2 13 J+1=8 3 19 4 28 5 16 6 35 7 51 8 4 9 65 N = 10 I < N (9) J < (N-1)-I (8)

28 After Iteration = 8 BUBBLE SORT I=1 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 6 J=7 1 2 2 13 J+1=8 3 19 4 28 5 16 6 35 7 4 8 51 9 65 N = 10 I < N (9) J < (N-1)-I (8)

29 Iteration = 9 BUBBLE SORT I=1 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 6 J=8 Condition false (J< 8) 1 2 2 13 J+1=8 3 19 4 28 5 16 6 35 7 4 8 51 9 65 N = 10 I < N (9) J < (N-1)-I (8)

30 Before Iteration = 1 BUBBLE SORT I=2 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 6 J=0 1 2 2 13 J+1=1 3 19 4 28 5 16 6 35 7 4 8 51 9 65 N = 10 I < N (9) J < (N-1)-I((10-1)-2=7)

31 After Iteration = 1 BUBBLE SORT I=2 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 2 J=0 1 6 2 13 J+1=1 3 19 4 28 5 16 6 35 7 4 8 51 9 65 N = 10 I < N (9) J < (N-1)-I((10-1)-2=7)

32 Before & After Iteration = 2
BUBBLE SORT I=2 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 2 J=1 1 6 2 13 J+1=2 3 19 4 28 5 16 6 35 7 4 8 51 9 65 N = 10 I < N (9) J < (N-1)-I((10-1)-2=7)

33 Before & After Iteration = 3
BUBBLE SORT I=2 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 2 J=2 1 6 2 13 J+1=3 3 19 4 28 5 16 6 35 7 4 8 51 9 65 N = 10 I < N (9) J < (N-1)-I((10-1)-2=7)

34 Before & After Iteration = 4
BUBBLE SORT I=2 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 2 J=3 1 6 2 13 J+1=4 3 19 4 28 5 16 6 35 7 4 8 51 9 65 N = 10 I < N (9) J < (N-1)-I((10-1)-2=7)

35 Before Iteration = 5 BUBBLE SORT I=2 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 2 J=4 1 6 2 13 J+1=5 3 19 4 28 5 16 6 35 7 4 8 51 9 65 N = 10 I < N (9) J < (N-1)-I((10-1)-2=7)

36 After Iteration = 5 BUBBLE SORT I=2 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 2 J=4 1 6 2 13 J+1=5 3 19 4 16 5 28 6 35 7 4 8 51 9 65 N = 10 I < N (9) J < (N-1)-I((10-1)-2=7)

37 Before & After Iteration = 6
BUBBLE SORT I=2 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 2 J=5 1 6 2 13 J+1=6 3 19 4 16 5 28 6 35 7 4 8 51 9 65 N = 10 I < N (9) J < (N-1)-I((10-1)-2=7)

38 Before Iteration = 7 BUBBLE SORT I=2 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 2 J=6 1 6 2 13 J+1=7 3 19 4 16 5 28 6 35 7 4 8 51 9 65 N = 10 I < N (9) J < (N-1)-I((10-1)-2=7)

39 After Iteration = 7 BUBBLE SORT I=2 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 2 J=6 1 6 2 13 J+1=7 3 19 4 16 5 28 6 4 7 35 8 51 9 65 N = 10 I < N (9) J < (N-1)-I((10-1)-2=7)

40 Before Iteration = 8 BUBBLE SORT I=2 Element Ar[10] Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 2 J<7 condition false, exit and increment the value of I J=7 1 6 2 13 J+1=7 3 19 4 16 5 28 6 4 7 35 8 51 9 65 N = 10 I < N (9) J < (N-1)-I((10-1)-2=7)

41 After Iteration = 1..6 BUBBLE SORT I=3 Element Ar[10] After Before Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 2 2 J=0..5 1 6 6 2 13 13 J+1=1..6 3 16 19 4 19 16 5 4 28 6 28 4 7 35 35 8 51 51 9 65 65 N = 10 I < N (9) J < (N-1)-I((10-1)-3=6)

42 After Iteration = 1..5 BUBBLE SORT I=4 Element Ar[10] After Before Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 2 2 J=0..4 1 6 6 2 13 13 J+1=1..5 3 16 16 4 4 19 5 19 4 6 28 28 7 35 35 8 51 51 9 65 65 N = 10 I < N (9) J < (N-1)-I((10-1)-4=5)

43 After Iteration = 1..4 BUBBLE SORT I=5 Element Ar[10] After Before Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 2 2 J=0..3 1 6 6 2 13 13 J+1=1..4 3 4 16 4 16 4 5 19 19 6 28 28 7 35 35 8 51 51 9 65 65 N = 10 I < N (9) J < (N-1)-I((10-1)-5=4)

44 After Iteration = 1..4 BUBBLE SORT I=6 Element Ar[10] After Before Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 2 2 J=0..2 1 6 6 2 4 13 J+1=1..3 3 13 4 4 16 16 5 19 19 6 28 28 7 35 35 8 51 51 9 65 65 N = 10 I < N (9) J < (N-1)-I((10-1)-6=3)

45 After Iteration = 1..2 BUBBLE SORT I=7 Element Ar[10] After Before Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 2 2 J=0..1 1 4 4 2 6 6 J+1=1..2 3 13 13 4 16 16 5 19 19 6 28 28 7 35 35 8 51 51 9 65 65 N = 10 I < N (9) J < (N-1)-I((10-1)-7=2)

46 After Iteration = 1 BUBBLE SORT I=8 Element Ar[10] After Before Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 2 2 J=0..0 1 4 4 2 6 6 J+1=1..1 3 13 13 4 16 16 5 19 19 6 28 28 7 35 35 8 51 51 9 65 65 N = 10 I < N (9) J < (N-1)-I((10-1)-8=1)

47 After Iteration = 1 BUBBLE SORT I=9 Element Ar[10] I<9 condition false, exit from the for loop After Index for(I=0;I<N;I++) { for(J=0;J<(N-1)-I;J++) { If (Ar[J]>Ar[J+1]) { temp=Ar[J]; Ar[J]=Ar[J+1]; Ar[J+1]=temp; } } } 2 J= 1 4 2 6 J+1= 3 13 4 16 5 19 6 28 7 35 8 51 9 65 N = 10 I < N (9) J < (N-1)-I((10-1)-9=0)


Download ppt "Ambily Krishnan PGT CS KV, Thrissur"

Similar presentations


Ads by Google