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

Program 5.11 Is Bubble Sort Algorithm:: Sorting. Exercise 1: Simple or Quadratic Sorting Algorithms

This document discusses sorting and searching algorithms. It provides code for bubble sort, selection sort, and insertion sort algorithms. It also provides code for sequential search and binary search functions. The code is tested on sample data sets to sort and search for values. Issues with bubble sort efficiency are discussed and the code is improved by adding a check to stop subsequent passes if no swaps occurred. Steps are shown applying selection sort, insertion sort, sequential search and binary search on data sets.

Uploaded by

teuku hanif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views10 pages

Program 5.11 Is Bubble Sort Algorithm:: Sorting. Exercise 1: Simple or Quadratic Sorting Algorithms

This document discusses sorting and searching algorithms. It provides code for bubble sort, selection sort, and insertion sort algorithms. It also provides code for sequential search and binary search functions. The code is tested on sample data sets to sort and search for values. Issues with bubble sort efficiency are discussed and the code is improved by adding a check to stop subsequent passes if no swaps occurred. Steps are shown applying selection sort, insertion sort, sequential search and binary search on data sets.

Uploaded by

teuku hanif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

SORTING.

EXERCISE 1: SIMPLE OR QUADRATIC SORTING ALGORITHMS


Program 5.11 is Bubble Sort algorithm:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

// Program 5.11
// Bubble Sort
void BubbleSort(dataType data[], intlistSize)
{ int pass, tempValue;
for ( pass =1;pass <listSize; pass++ )
{
// moves the largest element to the
// end of the array
for (int x = 0; x <listSize - pass; x++)
//compare adjacent elements
if ( data[x]>data[x+1] )
{//swap elements
tempValue = data[x];
data[x] = data[x+1];
data[x+1] = tempValue;
}// end if
} // end for

a. Using the following set of data, show step by step of implementing BubbleSort in Program
5.11 on the data. Compare the efficiency of the algorithm.

[ 3 5 7 6 9 8 ] dan[ 8 9 6 7 5 3 ]

Answer :[ 3 5 7 6 9 8 ] Best case -

8 8 8 8 8 9
9 9 9 9 9 8
6 6 6 7 7 7
7 7 7 6 6 6
5 5 5 5 5 5
3 3 3 3 3 3
9
8
7
6

9
8
7
6

5
3

5
3

Pass 5

Pass 1
9 9 9 9 9
8 8 8 8 8
7 7 7 7 7
6 6 6 6 6
5 5 5 5 5
3 3 3 3 3

Pass 2
9 9 9 9
8 8 8 8
7 7 7 7
6 6 6 6
5 5 5 5
3 3 3 3

Pass 3
9

Pass 4

The number of comparisons to sort data in this list:


(6-1) + (6-2) + (6-3) + (6-4)+(6-5) = 5+4 + 3 + 2 + 1 = 15.

[ 8 9 6 7 5 3 ] worst case
3 3 3 3 3 9
5 5 5 5 9 3
7 7 7 9 5 5
6 6 9 7 7 7
9 9 6 6 6 6
8 8 8 8 8 8
9 9
8 8
7 7
6 6
3 5
5 3

Pass 5

Pass 1
9 9 9 9 9
3 3 3 3 8
5 5 5 8 3
7 7 8 5 5
6 8 7 7 7
8 6 6 6 6

Pass 2
9 9 9 9
8 8 8 8
3 3 3 7
5 5 7 3
7 7 5 5
6 6 6 6

Pass 3
9 9 9

Pass 4

8 8 8
7 7 7
3 3 6
5 6 3
6 5 5

The number of comparisons to sort data in


this list:
(6-1) + (6-2) + (6-3) + (6-4)+(6-5) = 5+4 + 3 + 2 + 1 = 15.

b.Based on your answer given in a, give reason why Bubble Sort algorithm in Program 5.11 is
not efficient. Explain how the program can be improved.

Answer :it can be seen that the number of comparison for worse case and best case is the same with 15 comparisons.The difference can be seen in the number of swapping elements. Worse
case has maximum number of swapping: 15, while best case has 1 swapping since all
data is already in the right position.It can be concluded that in any pass, if there is no exchange
of data occur, the list is already sorted. The next pass shouldn't be continued and the
sorting process should stop.

c. Rewrite Program 5.11 in order to improve the efficiency.


Answer :
// Program 5.11
// Bubble Sort
void BubbleSort(dataType data[], intlistSize)
{ inttemp;
Bool sorted = false;
for (int pass =1;pass(pass<n)&&!sorted;pass++ )
{sorted = true;
for (int x = 0; x <n-pass; x++)
{
if ( data[x]>data[x+1] )
{
temp = data[x];
data[x] = data[x+1];
data[x+1] = temp;
sorted = false;
}
}
}
}

D . Given the following data set:

[5389172]
Show step by step the process of sorting the data using the following sorting techniques:
Selection Sort.
Insertion Sort
Answer : Selection Sort
Start - unsorted
5
3
Pass 1
5
3
5
3
Pass 2
5
3
5
3
Pass 3
5
3
5
3
Pass 4
5
3
3
1
Pass 5
3
1
1
2
Pass 6
1
2
1
2

8
8

9
1

1
7

7
2

2
9

8
1

1
7

7
2

2
8

9
9

1
1

7
2

2
7

8
8

9
9

1
2

2
5

7
7

8
8

9
9

2
3

5
5

7
7

8
8

9
9

3
3

5
5

7
7

8
8

9
9

Answer :Insertion Sort


5
5
3
3
3
3
3
3
1
1
1
1
1

3
3
5
5
5
5
5
5
3
3
3
3
2

8
8
8
8
8
8
8
8
5
5
5
5
3

9
9
9
9
9
9
9
9
8
8
7
7
5

1
1
1
1
1
1
1
1
9
9
8
8
7

7
7
7
7
7
7
7
7
7
7
9
9
8

2
2
2
2
2
2
2
2
2
2
2
2
9

Start - Unsorted
Insert 3 before 5
Keep 8
Keep 9
Insert 1 before 3
Insert 7 before 8
Insert 2 before 3

SEARCHING.
LAB 1: BASIC SEQUENTIAL SEARCH
Given the following Program 6.4, run the program to perform the tasks given
below.
[Diberikan Program 6.4 berikut,
laksanakanaturcaratersebutuntukmelakukantugas
yangdiberikan di bawah.]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

// Program 6.4
#include<iostream.h>
intCarianJujukan(int [], int, int);
void main()
{ int nom[100];
int k, target,j;
char ans=y;
cout<<"Masukkansaiztatasusunan:";
cin>>k;
for(int i=0; i<k;i++)
{ cout<<"nom["<<i<<"]=";
cin>>nom[i];
}
do{
cout<<"\nMasukkannombor yang ingin
dicari :";
cin>>target;
j= CarianJujukan(nom,k,target);
if (j==-1)
cout<<"gagal"<<endl;
else
cout<<"berjayaiaituunsur
nom[ "<<j<<"]\n";
cout<<Carinombor yang lain?:;
cin>>ans;
}while (ans==y);
}
intCarianJujukan(int a[ ], int n, int target)
{ int i;
// Program 6.4
#include<iostream.h>
intCarianJujukan(int [], int, int);
for (i=0; i<n; i++)
If (a[i]==target)
Return i;
Return -1;
}

a. Read the input for nom array which has the following 10 ascending numbers:
8 4 10 5 20 4 15 23 12 11
Answer :
nom [0] = 8
nom [1] = 4
nom [2] = 10
nom [3] = 5
nom [4] = 20
nom [5] = 4
nom [6] = 15
nom [7] = 23
nom [8] = 12
nom [9] = 11

b. What will the output be when performing a search for the key value 5?
Answer :Berjaya iaitu unsur[3]
Answer :Berjaya iaitu unsur [1]
d. Then, what will the output be when performing a search for the key value 25?
Answer :Gagal

LAB 2: BINARY SEARCH


[MAKMAL 2: CARIAN PERDUAAN]
Refer to the given Program 6.4 in Lab 1, replace the Carian Jujukan() function
with
Carian Pduaan()function given below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

// Program 6.5
intCarianPduaan (int a[ ], int n, int target)
{ int first=0;
int last=n-1;
int mid;
while (first <= last)
{ mid = (first + last) /2;
if(target== a[mid])
return mid;
else if(target< a[mid])
{
last=mid-1;
cout<<"nilaitengah:"<<mid<<"\tmula:"
<<first<<"\takhir:"<<last<<endl;
}
else
{
First = mid +1;
Cout<<nilaitengah:<<mid<<\tmula:
<<first<<\takhir:<<last<<endl;
}
}
return -1;
}

Based on Program 6.4 and the function in Program 6.5, perform the following
tasks:

a. To read nom array input with the following 10 ascending numbers:


2 5 6 8 10 12 15 16 20 21
Answer :
nom [0] = 2
nom [1] = 5
nom [2] = 6
nom [3] = 8
nom [4] = 10
nom [5] = 12
nom [6] = 15
nom [7] = 16
nom [8] = 20
Nom[9]=21

b. To perform search with the value of search key 5, what will be the output?
Answer:
nilai tengah: 4 mula : 0 akhir :3
berjaya iaitu unsur [1]

c. Next, perform search with search key value 20, what will be the output?
Answer: nilai tengah :4mula : 5 akhir : 9
Nilai tengah: 7 mula : 8 akhir : 9
Berjaya iaitu unsur[8]

d. Next, perform search with search key value 25, what will be the output?
Answer : nilai tengah:4 mula : 5 akhir : 9
Nilai tengah: 7 mula : 8 akhir : 9
Nilai tengah: 8 mula : 9 akhir : 9
Nilai tengah: 9 mula : 10 akhir : 9
gagal

You might also like