Programare 1
Programare 1
Programare 1
The searching function that will return the position from the array
if the element will be found, or value (-1) if we did not found the
value (using divide et impera):
int CautareBinara(int *p, int inc, int sfr, int val)//recursive
{
int mij;
mij = (inc + sfr)/2;
if(p[mij] == val)
return mij;
if(inc <= sfr) {
if(p[mij] > val)
sfr = mij - 1;
else
inc = mij + 1;
return CautareBinara(p, inc, sfr, val);
}
return -1;
#define DIM 12
int cmp(char *arg1, char *arg2);
int addelem(char *key, char **tab, int nelem);
void main(void)
{
char *luni[DIM] = {"ian", "feb", "mar", "apr", "mai", "iun" };
int i, nluni=6;
char *key = "iul";
if (addelem(key, luni, nluni))
printf("Luna %s este deja in tablou.\n", key);
else {
nluni++;
printf("Luna \"%s\" a fost adaugata in tablou : ", key);
for (i = 0; i < nluni; i++)
printf("%s, ", luni[i]);
}
}
13
Selection sorting:
Insertion sorting:
Interclassing sorting:
No comparison sorting:
- Bubble Sort
- Cocktail Sort
- Comb Sort
- Quick Sort
- Selection Sort
- Heap Sort
- Insertion Sort
- Shell Sort
- Merge Sort
-Radix sort
14
15
Example bubble-sort:
Example : 9 7 5 6 2
7 9 5 6 2 -> 7 5 9 6 2 -> 7 5 6 9 2 -> 7 5 6 2 9
5 7 6 2 9 -> 5 6 7 2 9 -> 5 6 2 7 9
5 6 2 7 9 -> 5 2 6 7 9
5 2 6 7 9 -> 2 5 6 7 9
Remark: after the first iteration on the last position
will be the greatest value, at the second iteration on
the penultimate position will be the greatest value
from the remaining array, etc
17
18
Example : 9 2 5 6 7:
9 2 5 6 7 -> 2 9 5 6 7 -> 2 5 9 6 7 -> 2 5 6 9 7
-> 2 5 6 7 9
25679
20
// parcurgeri
{
// cautare pozitie cel mai mic element din sirul curent
pozmin = i;
for(j=i+1; j<n; j++) {
if(p[pozmin] > p[j])
pozmin = j;
}
// interschimbare cu elementul de pe prima pozitie
temp = p[pozmin];
p[pozmin] = p[i];
p[i] = temp;
}
}
23
25
Remark:
The position where will be transferred the new
element is sequential searched (on an ordered
sub-array), so the algorithm may be improved
(analyzing elements being sorted), by binary
search
27
28
30
h=4
7
*
2
2
2
*
2
2
10
2
*
7
10
*
5
9
*
6
5
*
3
7
*
4
5
*
10
10
6
*
9
10
*
5
4
*
7
3
*
10
32
h=1
2
1
*
10
10
4
*
6
10
5
*
6
10
7
*
9
10
33
34
35
36
Example 2
#include <iostream>
using namespace std;
38
Example 3
#include <iostream>
using namespace std;
}
39
40
Homework:
Modify these sorting function to include the
comparison function in the calling of sorting function
void Sort(char **tab, int n, int(*fcmp)(char *s1, char *s2));
41
42
45
#include<iostream>
using namespace std;
void interclas(int *a,int i,int m,int j);
// i- pozitia de inceput a primului subsir, m- pozitia de sfarsit a primului
subsir,
// j- pozitia de sfarsit al celui de-al doilea subsir
void divimp(int *a,int i,int j);
// i- indicele primului element din sir,
// j - indicele ultimului element din sir
#define DIM 1000
void main()
{
int a[DIM],n;
cout<<"n="; cin>>n;
for(int i=0;i<n;i++)
{
cout<<"a["<<i<<"]=";
cin>>a[i];
}
46
divimp(a,0,n-1);
for(int i=0;i<n;i++)
cout<<a[i]<<' ';
} //main
48
Remarks:
The number of elementary executed operations
are of order O(nlog(n)).
For arrays with a huge number of components
the computing time is lower than the time used
for simple sorting algorithms as selection,
insertion or bubble where the complexity is of
order O(n2).
QuickSort algorithm
Is based on the partition notion
Partitioning data represents to divide data in 2
groups, one group with values greater than an
imposed value, pivot, the other group with
values less than pivot.
Steps of partitioning:
We consider an element x of the array named
pivot element
We browse the array from left till we find an
element ai greater than x
We browse the array from right till we find an
element aj less than x
50
we interchange ai with aj
We update i and j by increment, respective by
decrement
We repeat the previous steps till the browsing
are meet somewhere in the middle of the array
In this moment the array is partitioned:
In the left side of x are only elements less than x
In the right side of x are only elements greater than x
a[k] <= x,
k = 0,...,i-1
a[k] >= x,
k = j+1,...,n
a[k] = x,
k = j+1,...,i-1
51
QuickSort is
{
If (right-left) == 0 then
Return
Else
pivot = Tablou[right];
partition = Partitionare(left, right, pivot)
QuickSort(left, partition-1);
QuickSort(partition+1, right);
EndIf
}
52
48
88
57
71
60
42
83
73
48
65
48
57
71
60
42
83
73
88
65
57
42
48
57
48
42
42
48
60
42
60
71
71
83
73
88
65
65
73
88
83
57
60
65
71
73
83
88
57
60
65
71
73
83
88
57
71
60
42
83
73
48
65
48
57
71
60
42
83
73
88
65
48
57
42
60
71
83
73
88
65
48
57
42
60
48
57
42
48
57
42
57
42
48
65
83
60
65
71
73
42
60
65
71
73
83
88
48
60
65
71
73
83
88
60
65
71
73
83
88
57
73
88
71
88
83
55
Usual values:
last element from the partitioned array
Median value between the first, last and the
middle element of the array (half of the values
from the array are less than the median value
and half are greater)
57
58
59
60
61
void main(void)
{
struct pers angaj[ ] = {
{"x1", {1980, 6,6}},
{"x2", {1960, 5, 5}},
{"x3", {1960, 1,5}},
{"x4", {1961, 12, 32}},
{"x5", {1980, 2, 29}}
};
int i;
int nang = sizeof(angaj)/sizeof(struct pers);
// apel functie de sortare
qsort((void *)angaj, nang, sizeof(angaj[0]), (int (*)(const void*, const
void*))cmp);
printf("Datele sortate :\n");
for (i = 0; i < nang; i++) {
printf("\t%s, %d, %d, %d\n", angaj[i].numep, angaj[i].datan.an,
angaj[i].datan.luna, angaj[i].datan.zi);
}
_getch();
}
62
64