Vous êtes sur la page 1sur 23

Array : Sorting

Nisrin Binti Ahmad Ramly © nar


Sorting an Array
• Many situation in life we need to present a list of
data in order.
• For example, we might need to sort a list of integers
in ascending order.

Sorting
an Array

Bubble Insertion
Bubble Sort
• Idea:
– Repeatedly pass through the array
– Swaps adjacent elements that are out
of order
i+1

i
0 1 2 n

8 4 6 9 2 3 1
j j+1

• Easier to implement, but slower


than Insertion sort
#include<stdio.h>
Code void main ()
{
Bubble
int array [ ] = {8,4,6,9,2};
Sort int n = 5 , i , j , temp;

for( i=0; i<n; i++)


{ for(j=0; j<n-1; j++)
{ if(array[j]>array[j+1])
{
int temp = array[j+1];
array[j+1] = array[j];
array[j] = temp;
}
}
}
printf(‘’After Sorting:’’);
for(i=0; i<n; i++)
{
printf(" array [%d] = %d\n", i , array [ i ] );
}
}
Trace Code Bubble Sort
i++ j++ If(array [ j ] > Temp = array[ j+1] = array[ j ] =
i<5 j<4 array [ j+1 ] ) array[ j+1 ] array[ j ] temp 0 1 2 3 4
0 0 array[0] = 8 temp = array[1] = array[0] = 8 4 6 9 2
> array[1] array[0] temp
0 1 2 3 4
array[1] = 4
8 6 9 2
4 0 3
1 2 4

Yes 8 8 6 9 2
0 1 2 3 4
4 4 8 6 9 2

1 array[1] = 8 temp = array[2] = array[1] = 0 1 2 3 4


> array[2] array[1] temp 4 8 6 9 2
array[2] = 4
0 1 2 3 4

4 8 9 2
Yes 6 0 1 2 3 4

8 4 8 9 2
0 1 2 3 4
6
4 6 8 9 2
Trace Code Bubble Sort
i++ j++ If(array [ j ] > Temp = array[ j+1] = array[ j ] =
i<5 j<4 array [ j+1 ] ) array[ j+1 ] array[ j ] temp
2 array[2] = 8 - - -
> 0 1 2 3 4
array[3] = 9
4 6 8 9 2
No

3 array[3] = 9 temp = array[4] = array[3] = 0 1 2 3 4


> array[4] array[3] temp
array[4] = 2 4 6 8 9 2
0 1 2 3 4
Yes
2 4 6 8 9
0 1 2 3 4
9 4 6 8 9
0 1 2 3 4

4 6 8 2 9
2
Trace Code Bubble Sort
i++ j++ If(array [ j ] > Temp = array[ j+1] = array[ j ] =
i<5 j<4 array [ j+1 ] ) array[ j+1 ] array[ j ] temp
4 - - - -
0 1 2 3 4
No
4 6 8 2 9
Exit
Loop
1 0 array[0] = 4 - - -
0 1 2 3 4
>
array[1] = 6 4 6 8 2 9
No
1 array[1] = 6 - - -
> 0 1 2 3 4
array[2] = 8
4 6 8 2 9
No
Trace Code Bubble Sort
i++ j++ If(array [ j ] > Temp = array[ j+1] = array[ j ] =
i<5 j<4 array [ j+1 ] ) array[ j+1 ] array[ j ] temp 0 1 2 3 4
2 array[2] = 8 temp = array[3] = array[3] =
4 6 8 2 9
> array[3] array[2] temp
array[3] = 2 0 3
1 2 4

Yes 2 4 6 8 9

0 1 2 3 4
8
4 6 8 9

0 1 2 3 4

2 4 6 2 8 9
Trace Code Bubble Sort
i++ j++ If(array [ j ] > Temp = array[ j+1] = array[ j ] =
i<5 j<4 array [ j+1 ] ) array[ j+1 ] array[ j ] temp
3 array[3] = 8 - - - 0 1 2 3 4
>
array[4] = 9 4 6 2 8 9

No
4 Exit loop j
2 0 array[0] = 4 - - -
0 1 2 3 4
>
array[1] = 6 4 6 2 8 9

No
Trace Code Bubble Sort
i++ j++ If(array [ j ] > Temp = array[ j+1] = array[ j ] =
0 1 2 3 4
i<5 j<4 array [ j+1 ] ) array[ j+1 ] array[ j ] temp
1 array[1] = 6 temp = array[2] = array[1] = 4 6 2 8 9
> array[2] array[1] temp 0 1 2 3 4
array[2] = 2
4 6 8 9
Yes 2
0 1 2 3 4

4 6 8 9
6
0 1 2 3 4

2 4 2 6 8 9

2 array[2] = 6 - - -
> 0 1 2 3 4
array[3] = 8
4 2 6 8 9
No
Trace Code Bubble Sort
i++ j++ If(array [ j ] > Temp = array[ j+1] = array[ j ] =
i<5 j<4 array [ j+1 ] ) array[ j+1 ] array[ j ] temp
0 1 2 3 4
3 array[3] = 8 - - -
> 4 2 6 8 9
array[4] = 9

No
4 - - - -
0 1 2 3 4
No
4 2 6 8 9
Exit
Loop
Trace Code Bubble Sort
i++ j++ If(array [ j ] > Temp = array[ j+1] = array[ j ] =
i<5 j<4 array [ j+1 ] ) array[ j+1 ] array[ j ] temp
0 1 2 3 4
3 0 array[0] = 4 temp = array[1] = array[3] =
> array[1] array[0] temp 4 2 6 8 9
array[1] = 2
0 1 2 3 4
Yes
4 6 8 9
2
0 1 2 3 4

4 4 6 8 9

0 1 2 3 4

2 4 6 8 9
2
1 array[1] = 4 - - -
>
array[2] = 6 0 1 2 3 4

2 4 6 8 9
No
Trace Code Bubble Sort
i++ j++ If(array [ j ] > Temp = array[ j+1] = array[ j ] =
i<5 j<4 array [ j+1 ] ) array[ j+1 ] array[ j ] temp
2 array[2] = 6 - - - 0 1 2 3 4
>
array[3] = 8 2 4 6 8 9

No
3 array[3] = 8 - - -
> 0 1 2 3 4
array[4] = 9
2 4 6 8 9
No

4 Exit loop j
4 0 array[0] = 2 - - -
> 0 1 2 3 4
array[1] = 4
2 4 6 8 9
No
Trace Code Bubble Sort
i++ j++ If(array [ j ] > Temp = array[ j+1] = array[ j ] =
i<5 j<4 array [ j+1 ] ) array[ j+1 ] array[ j ] temp
1 array[1] = 4 - - - 0 1 2 3 4
>
array[2] = 6 2 4 6 8 9

No
2 array[2] = 6 - - -
> 0 1 2 3 4
array[3] = 8
2 4 6 8 9
No
3 array[3] = 8 - - -
> 0 1 2 3 4
array[4] = 9
2 4 6 8 9
No
4 - - - -

No 0 1 2 3 4

Exit 2 4 6 8 9
Loop
5 Exit loop i
Insertion Sort
 Idea: like sorting a hand of playing cards
1. Start with an empty left hand and the
cards facing down on the table.
2. Remove one card at a time from the
table, and insert it into the correct
position in the left hand
• compare it with each of the cards
already in the hand, from right to left
3. The cards held in the left hand are
sorted
• these cards were originally the top
cards of the pile on the table
Insertion Sort
To insert 12, we need to
make room for it by moving
first 36 and then 24.
#include<stdio.h>
int main()
{ int i, j, num, temp, arr[20];

printf("Enter total elements: "); // prompt user enter total of elements


scanf("%d", &num);

printf("Enter %d elements: ", num); // prompt user enter value of elements

for (i = 0; i < num; i++) // Looping to user enter value of elements until
{ scanf("%d", &arr[i]); total of elements
printf(“\n”);

}
for (i = 1; i < num; i++) // Looping i until total of elements
{ temp = arr[i]; //temp hold the value of arr[i]
j = i - 1; // value of j less than 1 from value i

while ((temp < arr[j]) && (j >= 0)) //compare value of temp with arr [j]
{ arr[j + 1] = arr[j]; //arr[j+1] hold value of arr[j]
j = j - 1; // decremental j
}

arr[j + 1] = temp; // arr[j + 1] hold value of temp


}

printf("After Sorting: \n"); //Display ascending sorting value in array


for (i = 0; i < num; i++)
printf(“\n %d", arr[i]);

return 0;
}
Trace Code Insertion Sort

for (i = 1; i < num; i++) // Looping i until total of elements


{ temp = arr[i]; //temp hold the value of arr[i]
j = i - 1; // value of j less than 1 from value i

while ((temp < arr[j]) && (j >= 0))


//compare value of temp with arr [j]
{
arr[j + 1] = arr[j]; //arr[j+1] hold value of arr[j]
j = j - 1; // decremental j
}

arr[j + 1] = temp; // arr[j + 1] hold value of temp


}
Trace Code Insertion Sort
In memory computer:
 Total Element : 5 0 1 2 3 4
 Element : 8 4 6 9 2 8 4 6 9 2

i<5 Temp = arr j=i-1 temp<arr[j] && arr[j+1] = j=j-1 arr[j+1] = i++
[i] j>=0 arr[j] temp
Yes temp = 0 temp = 4 arr[1]= 8 -1 1 j0 1 2 3 4
arr[1] < 8 6 9 2
arr[0] = 8
4 0 1 2 3 4
Yes
8 6 9 2

arr[-1] = ? - - arr[0] = 2
4 0 1 2 3 4
No – exit loop 4 8 6 9 2
while
Trace Code Insertion Sort 0 1 2 3 4

i<5 Temp = arr j=i-1 temp<arr[j] && arr[j+1] = j=j-1 arr[j+1] = i++ 4 8 6 9 2
[i] j>=0 arr[j] temp
Yes temp = 1 temp = 6 arr[2]= 8 0 0 j1 2 3 4
arr[2] < 4 8 9 2
arr[1] = 8
0 1 2 3 4
6
Yes 4 8 9 2

temp = 6 - - arr[1] = 3
<
0 1 2 3 4
arr[0] = 4 6
4 6 8 9 2
No – exit loop
while
Yes temp = 2 temp = 9 - - arr[3] = 4
0 1 j2 3 4
arr[3] <
arr[2] = 8 9 4 6 8 2
9
0 1 2 3 4
No – exit loop
while 4 6 8 9 2
Trace Code Insertion Sort 2
0 1 3 4
i<5 Temp = arr j=i-1 temp<arr[j] && arr[j+1] = j=j-1 arr[j+1] = i++ 4 6 8 9 2
[i] j>=0 arr[j] temp
Yes temp = 3 temp = 2 arr[4]= 9 2 0 1 2 j3 4
arr[4] <
arr[3] = 9 4 6 8 9
2 0 1 2 3 4
Yes
4 6 8 9
temp = 2 arr[3]= 8 1
< 0 1 2 3 4
arr[2] = 8 4 6 8 9
Yes

temp = 2 arr[2]= 6 0
< 0 1 2 3 4
arr[1] = 6 4 6 8 9
Yes
Trace Code Insertion Sort 2
0 1 3 4
i<5 Temp = arr j=i-1 temp<arr[j] && arr[j+1] = j=j-1 arr[j+1] = i++ 4 6 8 9 2
[i] j>=0 arr[j] temp
Yes temp = 3 temp = 2 arr[4]= 9 2 0 1 2 j3 4
arr[4] <
arr[3] = 9 4 6 8 9
2 0 1 2 3 4
Yes
4 6 8 9
temp = 2 arr[3]= 8 1
< 0 1 2 3 4
arr[2] = 8 4 6 8 9
Yes

temp = 2 arr[2]= 6 0
< 0 1 2 3 4
arr[1] = 6 4 6 8 9
Yes
Trace Code Insertion Sort 2
0 1 3 4
i<5 Temp = arr j=i-1 temp<arr[j] && arr[j+1] = j=j-1 arr[j+1] = i++ 4 6 8 9
[i] j>=0 arr[j] temp
temp = 2 arr[1]= 4 -1
< 0 1 2 3 4
arr[0] = 4 4 6 8 9
Yes
j=-1 : j >=0 - - arr[0] = 5
0 1 2 3 4
No – exit loop 2 2 4 6 8 9
while
No –
exit
loop
i

Vous aimerez peut-être aussi