Mavzu: Saralash algoritmlari (insertion, selection, bubble sort) Variant 1 A masivning eleementlarini to`g`ridan to`g`ri qo`hish usuli bilan saralash algoritmidan foydalanib saralang va massining eng kata elementini ekranga chiqarish daturini tuzing
Dastur kodi
#include using namespace std;
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
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+1] > arr[j])
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 B[] = {6, 18, 97, 85, 66, 41, 78, 22, 36};
cout<<"\nTartiblanmagan massiv: \n";
cout<<" 6, 18, 97, 85, 66, 41, 78, 22, 36 ";
int n = sizeof(B)/sizeof(B[0]);
bubbleSort(B, n);
cout<<"\n\nTartiblangan massiv: \n";
printArray(B, n);
cout<<"\nmassivdagi eng katta element = "< return 0;
}