Programming and Data Structures



Download 108,35 Kb.
bet3/3
Sana16.06.2022
Hajmi108,35 Kb.
#677139
1   2   3
Bog'liq
Sorting 2

Insertion Sort


int main()
{
int x[ ]={-45,89,-65,87,0,3,-23,19,56,21,76,-50};
int i;
for(i=0;i<12;i++)
printf("%d ",x[i]);
printf("\n");
insertionSort(x,12);
for(i=0;i<12;i++)
printf("%d ",x[i]);
printf("\n");
}
OUTPUT
-45 89 -65 87 0 3 -23 19 56 21 76 -50
-65 -50 -45 -23 0 3 19 21 56 76 87 89

Selection Sort

General situation :


remainder, unsorted
smallest elements, sorted
0
size-1
k
x:
Steps :
    • Find smallest element, mval, in x[k…size-1]
    • Swap smallest element with x[k], then increase k.

0
k
size-1
mval
swap
x:

Selection Sort


/* Yield location of smallest element in
x[k .. size-1];*/
int findMinLloc (int x[ ], int k, int size)
{
int j, pos; /* x[pos] is the smallest element found so far */
pos = k;
for (j=k+1; jif (x[j] < x[pos])
pos = j;
return pos;
}

Selection Sort


/* The main sorting function */
/* Sort x[0..size-1] in non-decreasing order */
int selectionSort (int x[], int size)
{ int k, m;
for (k=0; k{
m = findMinLoc(x, k, size);
temp = a[k];
a[k] = a[m];
a[m] = temp;
}
}

Selection Sort - Example


-17
12
-5
6
142
21
3
45
x:
3
12
-5
6
142
21
-17
45
x:
-17
-5
12
6
142
21
3
45
x:
-17
-5
3
6
142
21
12
45
x:
-17
-5
3
6
142
21
12
45
x:
-17
-5
3
6
12
21
142
45
x:
-17
-5
3
6
12
21
45
142
x:
-17
-5
3
6
21
142
45
x:
12
-17
-5
3
6
12
21
45
142
x:

Bubble Sort


The sorting process proceeds in several passes.
  • In every pass we go on comparing neighbouring pairs, and swap them if out of order.
  • In every pass, the largest of the elements under considering will bubble to the top (i.e., the right).

In every iteration heaviest element drops at the bottom.
The bottom moves upward.
Download 108,35 Kb.

Do'stlaringiz bilan baham:
1   2   3




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish