Searching and Sorting
Searching and Sorting
Searching and Sorting
9.1. Introduction
Analysis
= (N + 1) / 2.
Sequential search is easy to write and efficient for
short lists. It does not require the list to be sorted.
However if the list is long, this searching method
becomes inefficient, as it has to travel through the
whole list. We can overcome this shortcoming by using
Binary Search Method.
Analysis
9.3 Sorting
Analysis:
1 42 11 11 11 11
2 23 23 23 23 23
3 74 74 74 42 42
4 11 42 42 74 65
5 65 65 65 65 74
9.3.2.Bubble sort
struct stud
{
char name [20];
float per _marks;
}p[SIZE];
2 23 42 42 42 42 42 11 11 23 23
3 74 : 74 11 11 : 42 42 : 42
4 11 : : 74 65 : : 65 : 65
5 65 : : : 74 : : 74 : 74
Pass 1 12 30 8 10 15 100 2 33 5 67
Pass 2 8 10 12 30 2 15 33 100 5 67
Pass 3 2 8 10 12 15 30 33 100 5 67
Pass 4 2 5 8 10 12 15 30 33 67 100
fig 3. Trace of merging process
Here we will write two functions, two_way_mergesort
(K,I,N) and simple_merge(K,first,second,third).
{
p = p+1;
temp[p] = K[j];
j = j+1;
}
}
else (first sublist has elements left) {
while( i < second)
{
p = p+1;
temp[p] = K[i];
i = i+1;
}
2. Test for a base condition when the sub table has the
size 1 recursive call must stop.
if(SIZE < = 1)
return;
3. Calculate the midpoint of the current sub table.
Middle = start +[SIZE/2]-1.
This is because start is not always 1 but changes
for different sub tables.
7. return;
# include<stdio.h>
readara()
{
int i, m;
printf ("give the number of elements in an array:");
scanf("%d",&m);
printara (int m)
{
int i;
for (i=0; i<m;i++)
printf("%d\t", b[i]);
}
printf(" ____________________________\n")
if (l<r)
{
f=l+1;
e =r;
do
{
while (b[l] > b[f] && f<=e)
f++;
while (b[l] <b[e] && f<=e)
e--;
if (f<e)
swap (f e);
}while (f-1, l);
if(l<f-2)
{
push (l);
push (f-2);
}
if (f<r)
{ push (f);
push (r);
}
}
}while (!stkempty());
}
pop()
{
top --;
return (stk[top+1]);
}
main()
{
int n;
clscr ();
n=readara();
qsort (n);
printf("n\n********* sorted array is .. *******\n");
printara(n);
}
**************** s o r t e d a r r a y i s … *******
3 6 11 20 27 44 45 70
_________________________________________________________
_____
Consider numbers
42 23 74 11 65 57 94 36 99 87 70 81
61
Pass 1: P0 P1 P2 P3 P4 P5 P6 P7 P8 P9
70 11 42 23 74 65 36 57 99
81 94 87
61
70 11 81 61 42 23 74 94 65 36 57 87
99
Pass 2: P0 P1 P2 P3 P4 P5 P6 P7 P8 P9
11 23 36 42 57 61 70 81 99
65 74 87
11 23 36 42 57 61 65 70 74 81 87 99
1. while( j <= N)
{
2. for( i = 0; i<=9; i++)
T[i]=B[i]=NULL; (initialization)
r=first;
3. while( r != NULL)
{
if(T[d] == NULL)
T[d]=B[d]=r;
else
{
T[d]->link = r;
}
r->link = NULL;
r=next;
4. p=0;
while(B[p] == NULL)
{
p = p+1;
}
first = B[p];
while(i=p+1; i<=9;i++)
{
prev =T[i-1];
}
if(T[i] != NULL)
{
5. return.
T[0] / 70 B[0]
T[1] / 61 81 11
B[1]
T[2] / 42 B[2]
T[3] / 23 B[3]
T[4] / 94 74 B[4]
T[5] / 65 B[5]
T[6] / 36 B[6]
T[7] / 87 57 B[7]
T[9] / 99 B[9]
PASS 1
T[1] / 11 B[1]
T[2] / 23 B[2]
T[3] / 36 B[3]
T[4] / 42 B[4]
T[5] / 57 B[5]
T[7] / 74 70 B[7]
T[8] / 87 81 B[8]
T[9] / 99 94 B[9]
PASS 2
Creation
General algorithm.
We will repeat all the steps below until there are still
records.
Create_heap(K[],N)
8. Return;
Input list : 42 23 74 11 65 03 94 36 99 87
Step 1:
1. Q=2
2. i=2 Key = 23
3. J=1
7. K[2]=23
42
23
Step 2:
74 k1
k2 23 42 k3
Step 3:
1. Q=4
2. i=4 Key = 11
3. J=2
4. false
7. K[4]=11
74 k1
k2 23 42 k3
k4 11
Step 4:
1. Q=5
2. i=54 Key = 65
3. J=2
4. true
5. K[5]=23
6. j=2
7. K[2]=65
74 k1
k2 65 42 k3
k4 11 23 k5
Step 5:
1. Q=6
k2 65 42 k3
k4 11 23 k5 3 k6
Step 6:
1. Q=7
2. i=7 Key = 94
3. J=3
4. true
5. K[7]=42
6. i=3 j=1
4. true
5. K[3]=74
6. i = 1 j =1
4. false
7. K[1]=94
94 k1
k2 65 74 k3
k4 11 23 k5 3 k6 42 k7
Step 7:
1. Q=8
2. i=87 Key = 36
3. J=4
4. true
5. K[8]=11
6. i=4 j=2
4. false
7. K[4]=36
94 k1
k2 65 74 k3
k4 36 23 k5 3 k6 42 k7
Step 8:
1. Q=9
2. i=9 Key = 99
3. J=4
4. true
5. K[9]=36
6. i=4 j=2
4. true
5. K[4]=65
6. i=2 j=1
4. true
5. K[2]=94
7. i=1 j=1
4. false
7. K[1]=99
99 k1
k2 94 74 k3
k4 65 23 k5 3 k6 42 k7
11 k8 36 k9
Step 9:
1. q=10
2. i=10 Key = 87
3. J=5
4. true
5. K[10]=23
6. i=5 j=2
4. false
7. K[5]=87
99 k1
k2 94 74 k3
11 k8 k9 36 23 k10
Sorting of Heap
Heap_Sort(K[],N)
4. I=1;
Key= K[1];
J=2;
K[i]=Key;
1. 99 k1
k2 94 74 k3
k4 65 87 k5 3 k6 42 k7
11 k8 k9 36 23 k10
Initial Heap
2. Q=10
3. K[1] = 23 K[10]=99
23 k1
k2 94 74 k3
k4 65 87 k5 3 k6 42 k7
11 k8 k9 36
99
8. I=2 J=4
9. if 5<10 /*true */
if 87 >65
I=J+1=5
10. K[2]=23 94 k1
23 k2
6. J<=9 && K[5]>23
7. K[2]=87
8. I=5 J=9
9. K[5]=23
94 k1
k2 87 74 k3
k4 65 23 k5 3 k6 42 k7
11 k8 k9 36
Step 2:
36 k1
k2 87 74 k3
k4 65 23 k5 3 k6 42 k7
94 99
K[9] K[10]
87 k1
k2 65 74 k3
k4 36 23 k5 3 k6 42 k7
11 k8
Step 3:
11 k1
k2 65 74 k3
k4 36 23 k5 3 k6 42 k7
87 94 99
K[8] K[9] K[10]
74 k1
k2 65 42 k3
Step 4:
11 k1
k2 65 42 k3
k4 36 23 k5 3 k6
74 87 94 99
K[7]K[8] K[9]
K[10]
65 k1
k2 36 42 k3
k4 11 23 k5 3 k6
step 5:
3 k1
k2 36 42 k3
k4 11 23 k5
65 74 87 94 99
K[6]K[7]K[8] K[9]
K[10]
42 k1
k4 11 23 k5
step 6:
23 k1
k2 36 3 k3
k4 11
42 65 74 87 94 99
K[5]K[6]K[7]K[8] K[9]
K[10]
36 k1
k2 23 3 k3
k4 11
step 7:
11 k1
k2 23 3 k3
36 42 65 74 87 94 99
K[4]K[5]K[6]K[7]K[8] K[9]
K[10]
k2 11 3 k3
step 8:
3 k1
k2 11
23 36 42 65 74 87 94 99
K[3]K[4]K[5] K[6]K[7]K[8] K[9]
K[10]
11 k1
k2 3
Step 9:
3
11 23 36 42 65 74 87 94 99
K[2]K[3]K[4]K[5] K[6]K[7]K[8] K[9]
K[10]