m x n matritsa berilgan. Matritsa elementlarini har bir satri bo’yicha saralash dasturini tuzing.
Dastur kodi:
#include
using namespace std;
#define SIZE 10
void sort(int mat[SIZE][SIZE], int n,int m)
{
int temp[n * m];
int k = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
temp[k++] = mat[i][j];
sort(temp, temp + k);
k = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
mat[i][j] = temp[k++];
}
void print(int mat[SIZE][SIZE], int n,int m)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cout << mat[i][j] << " ";
cout << endl;
}
}
int main()
{
int mat[SIZE][SIZE] ;
int n,m;
cout<<"matritsa satrlar soni ";
cin>>n;
cout<<"matritsa ustunlar soni ";
cin>>m;
for(int i=0;i
for(int j=0;j
cin>>mat[i][j];
}
}
cout << "biz kiritgan massiv";
print(mat, n,m);
sort(mat, n,m);
cout << "saralangan massiv";
print(mat, n,m);
return 0;
}
Bir o’lchovli massivni saralashni quyidagi saralash algoritmlari asosida dasturini tuzing:
Pufakcha usuli;
Dastur kodi
#include
using namespace std;
int main() {
int n;
cout<<"Massiv elementlar soni:";
cin>>n;
int a[n];
for (int i = 0; i < n; i++)
cin>>a[i];
for (int i = 0; i < n-1; i++) {
int minPos = i;
for (int j = i+1; j < n; j++)
if (a[j] < a[minPos])
minPos = j;
int t = a[i];
a[i] = a[minPos];
a[minPos] = t;
}
for (int i = 0; i < n; i++)
cout<
}
Tanlash usuli;
Dastur kodi
#include
#include
using namespace std;
void printElems(vector < int > list) {
int i;
int len = list.size();
for (i = 0; i < len; i++)
cout << list[i] << " ";
}
void selectionSort(vector < int > & a) {
int i, min, k, swap, location;
int len = a.size();
for (i = 0; i < len - 1; i++) {
min = a[i];
location = i;
for (k = i + 1; k < len; k++) {
if (min > a[k]) {
min = a[k];
location = k;
}
}
swap = a[i];
a[i] = a[location];
a[location] = swap;
cout << "\n\n saralash " << i + 1 ;
printElems(a);
}
}
int main() {
int i, num, n;
vector < int > v;
cout << " Massiv elementlari soni : ";
cin >> n;
cout << "\n\n";
for (i = 0; i < n; i++) {
cout << "element " << i + 1 << " : ";
cin >> num;
v.push_back(num);
}
cout << "\n\nSaralashdan oldin massiv elementlari : ";
printElems(v);
selectionSort(v);
cout << "\n\nSaralangan massiv elementlari : ";
printElems(v);
cout << "\n\n\n";
return 0;
}
Merge sort algoritmi qadamlari
Merge Sort funksiyasiga array va uning boshlang’ich (left) va oxirgi nuqtalari (right) beriladi.
Arraynining o’rtasi hisoblanadi: mid = (left + right)/2. Bu narsa uni teng ikkiga bo’lish uchun kerak bo’ladi.
Merge sortni rekursiv holda birinchi va ikkinchi qismlar uchun chaqiriladi.
Dastur kodi
#include
using namespace std;
void swapping(int &a, int &b) {
int temp;
temp = a;
a = b;
b = temp;
}
void display(int *array, int size) {
for(int i = 0; i
cout << array[i] << " ";
cout << endl;
}
void merge(int *array, int l, int m, int r) {
int i, j, k, nl, nr;
nl = m-l+1; nr = r-m;
int larr[nl], rarr[nr];
for(i = 0; i
larr[i] = array[l+i];
for(j = 0; j
rarr[j] = array[m+1+j];
i = 0; j = 0; k = l;
while(i < nl && j
if(larr[i] <= rarr[j]) {
array[k] = larr[i];
i++;
}else{
array[k] = rarr[j];
j++;
}
k++;
}
while(i
array[k] = larr[i];
i++; k++;
}
while(j
array[k] = rarr[j];
j++; k++;
}
}
void mergeSort(int *array, int l, int r) {
int m;
if(l < r) {
int m = l+(r-l)/2;
mergeSort(array, l, m);
mergeSort(array, m+1, r);
merge(array, l, m, r);
}
}
int main() {
int n;
cout << "Massiv elementlar soni: ";
cin >> n;
int arr[n];
cout << "elementlarni kiriting:" << endl;
for(int i = 0; i
cin >> arr[i];
}
cout << "Saralashdan oldin: ";
display(arr, n);
mergeSort(arr, 0, n-1);
cout << "Saralashdan keyin: ";
display(arr, n);
}
Tezkor saralash
Dastur kodi
#include
using namespace std;
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition (int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++)
{
if (arr[j] < pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
cout << "Sorted array: \n";
printArray(arr, n);
return 0;
}
Do'stlaringiz bilan baham: |