2) Pufaksimon saralash (Bubble sort)
#include
using namespace std;
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(arr[j], arr[j+1]);
}
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
cout<<"Saralangan massiv: \n";
printArray(arr, n);
return 0;
}
3) Shaker saralashi (Cocktail Sort)
#include
using namespace std;
void CocktailSort(int a[], int n)
{
bool swapped = true;
int start = 0;
int end = n - 1;
while (swapped)
{
swapped = false;
for (int i = start; i < end; ++i)
{
if (a[i] > a[i + 1]) {
swap(a[i], a[i + 1]);
swapped = true;
}
}
if (!swapped)
break;
swapped = false;
--end;
for (int i = end - 1; i >= start; --i)
{
if (a[i] > a[i + 1]) {
swap(a[i], a[i + 1]);
swapped = true;
}
}
++start;
}
}
void printArray(int a[], int n)
{
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}
int main()
{
int a[] = { 5, 1, 4, 2, 8, 0, 2 };
int n = sizeof(a) / sizeof(a[0]);
CocktailSort(a, n);
printf("Saralangan massiv :\n");
printArray(a, n);
return 0;
}
4) Taroqsimon saralash (Comb sort)
#include
using namespace std;
int getNextk(int k)
{
k = (k*10)/13;
if (k < 1)
return 1;
return k;
}
void combSort(int a[], int n)
{
int k = n;
bool swapped = true;
while (k != 1 || swapped == true)
{
k = getNextk(k);
swapped = false;
for (int i=0; i
{
if (a[i] > a[i+k])
{
swap(a[i], a[i+k]);
swapped = true;
}
}
}
}
int main()
{
int a[] = {8, 4, 1, 56, 3, -44, 23, -6, 28, 0};
int n = sizeof(a)/sizeof(a[0]);
combSort(a, n);
printf("Saralangan massiv: \n");
for (int i=0; i
printf("%d ", a[i]);
return 0;
}
5) Joylashtirish usuli bo’yicha saralash
#include
using namespace std;
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
int arr[] = { 12, 11, 13, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
2.MASALANING QO’YILISHI
2.3. Sizga bir o’lchamli butun sonlardan iborat massiv berilgan. Sizning vazifangiz bu massiv elementlarini modullari jihatdan kamaymaslik tartibida saralaydigan dastur tuzish. Agar modul jihatdan teng musbat va manfiy sonlar mavjud bo’lsa manfiy son oldinroq joylashtirilsin:
Masalan:
9 8 -9 2 -4 3
2 3 -4 8 -9 9
3.AMALIY QISM
(Pufakcha usulida saralash)
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
38
39
40
41
42
43
44
45
46
47
|
#include
//3-laboratoriya @TBCLbot
using namespace std;
int main()
{ int k=12;
srand(time(NULL));
int A[k],k1=k,k2=k1;
for(int i=0;i
{
A[i]=rand()%10;
if(i%4==0)
A[i]=-A[i];
cout<<" "<
if(i%4==0)
cout<<"\n";
}
bool b=true;
while(b)
{
b=false;
for(int i=0;i+1
{
if(fabs(A[i])>fabs(A[i+1]))
{
swap(A[i],A[i+1]);
b=true;
}}
k--;}
b=true;
while(b)
{
b=false;
for(int i=0;i+1{
if(fabs(A[i])==fabs(A[i+1]))
{
if(A[i]>=A[i+1])
swap(A[i],A[i+1]);b=true;
}}
k2--;}
cout<<"\n================="<
for(int i=0;i
{
if(i%6==0)
cout<
return 0;
}
|
4.XULOSA
5. XULOSA
Qo’yilgan masalani yechish uchun chiziqli va takrorlanuvchi algoritmlardan foydalanildi.Masalani yechishda Pufaksimon saralashdan foydalanildi. K ta elementga ega A static massiv e’lon qilindi va rand() funksiyasi orqali massiv to’ldirildi. Masala ,shartini bajarishda asosiy qism 17-40-satrlardagi algoritim orqali cpp da ishlandi. Dast avval massivni modul jihatidan pufaksimon usulda saralandi va undan so’ng yana pufaksimon usulda modul jihatidan teng bo’lgan massiv elementlar agar birinchi o’rindagi massiv elementi agar ikkinchi o’rindagi massiv elementidan kichik bo’lsa massiv elementlari almashtirildi. Algoritimimizni baholasag :
5.FOYDALANILGAN ADABIYOTLAR.
Foydalanilgan saytlar:
https://www.geeksforgeeks.org/bubble-sort/
https://www.softwaretestinghelp.com/bubble-sort/
https://www.programiz.com/dsa/bubble-sort
Foydalanilgan kitoblar:
Do'stlaringiz bilan baham: |