Saralash algoritmi tavsiloti: tanlash usuli bilan saralash pufakchali usuli bilan saralash aralash usuli bilan saralash shell usuli bilan saralash piramida usuli bilan saralash tez tartiblash usuli bilan saralash 9 birlashtirish usuli bilan saralash


Download 193.57 Kb.
bet2/2
Sana19.06.2023
Hajmi193.57 Kb.
#1612729
1   2
Bog'liq
OYGUL

TANLASH USULI BILAN SARALASH



Tanlash dasturi C# tilida:

using System;

class Program


{
static void Main(string[] args)
{
int n = 0;
int[] arr;
int minIndex, temp;

Console.Write("n ni kiriting: ");


n = int.Parse(Console.ReadLine());

arr = new int[n];



for (int i = 0; i < n; i++)
{
Console.Write((i + 1) + "-elementni kiriting: ");
arr[i] = int.Parse(Console.ReadLine());
}

for (int i = 0; i < n - 1; i++)
{
minIndex = i;

for (int j = i + 1; j < n; j++)
{
if (arr[j] < arr[minIndex])
{
minIndex = j;
}
}

temp = arr[i];


arr[i] = arr[minIndex];
arr[minIndex] = temp;
}

Console.Write("Saralangan ketma-ketlik: ");


for (int i = 0; i < n; i++)
{
Console.Write(arr[i] + " ");
}

Console.ReadLine();


}
}
NATIJA:

PUFAKCHALI USULI BILAN SARALASH


  • Agar ikki qo’shni element noto’g’ri tartibda joylashib qolgan bo’lsa, ularning o’rnini almashtiramiz.

  • Umumiy n-1 marta jarayon bajariladi. Har safar ikkita qo’shni element taqqoslanadi.

  • Elementlar o’z o’rinlariga pufakga o’xshab siljib boradi.



Pufakchali dasturi c# tilida:
using System;

class Program {


static void Main(string[] args) {
int n;
n = Convert.ToInt32(Console.ReadLine());
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = Convert.ToInt32(Console.ReadLine());
for (int i = n - 1; i >= 1; i--) {
for (int j = 0; j < i; j++) {
if (a[j] > a[j + 1]) {
int t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
for (int i = 0; i < n; i++)
Console.Write(a[i] + " ");
Console.ReadLine();
}

}
NATIJA:




ARALASH USULI BILAN SARALASH



Aralash dasturi c# tilida:
using System;

class Program {


static void Main(string[] args) {
int n;
n = Convert.ToInt32(Console.ReadLine());
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = Convert.ToInt32(Console.ReadLine());
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++)
Console.Write(a[i] + " ");
Console.ReadLine();
}
}
NATIJA:



SHELL USULI BILAN SARALASH




Shell dasturi c# tilida:
using System;

class Program {


static int[] a = new int[100001];

static void qsort(int L, int R) {


int m = (L + R) / 2;
int X = a[m];
int i = L;
int j = R;
while (i <= j) {
while (a[i] < X) i++;
while (a[j] > X) j--;
if (i <= j) {
int t = a[i]; a[i] = a[j]; a[j] = t;
i++;
j--;
}
}
if (L < j) qsort(L, j);
if (i < R) qsort(i, R);
}

static void Main(string[] args) {


int n = int.Parse(Console.ReadLine());
string[] input = Console.ReadLine().Split();
for (int i = 0; i < n; i++)
a[i] = int.Parse(input[i]);
qsort(0, n - 1);
for (int i = 0; i < n; i++)
Console.Write(a[i] + " ");
Console.ReadLine();
}
}
NATIJA:


PIRAMIDA USULI BILAN SARALASH



Piramida dasturi c# tilida:
using System;

class Program {


static void Main(string[] args) {
int[] arr = { 5, 3, 8, 4, 2, 1, 9, 7, 6 };
Console.WriteLine("Before sorting:");
PrintArray(arr);
PyramidSort(arr);
Console.WriteLine("After sorting:");
PrintArray(arr);
}

static void PyramidSort(int[] arr) {


int n = arr.Length;

// Build heap (rearrange array)


for (int i = n / 2 - 1; i >= 0; i--)
Heapify(arr, n, i);

// One by one extract an element from heap


for (int i = n - 1; i >= 0; i--) {
// Move current root to end
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;

// call max heapify on the reduced heap


Heapify(arr, i, 0);
}
}

static void Heapify(int[] arr, int n, int i) {


int largest = i; // Initialize largest as root
int l = 2 * i + 1; // left = 2*i + 1
int r = 2 * i + 2; // right = 2*i + 2

// If left child is larger than root


if (l < n && arr[l] > arr[largest])
largest = l;

// If right child is larger than largest so far


if (r < n && arr[r] > arr[largest])
largest = r;

// If largest is not root


if (largest != i) {
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;

// Recursively heapify the affected sub-tree


Heapify(arr, n, largest);
}
}

static void PrintArray(int[] arr) {


foreach (int i in arr)
Console.Write(i + " ");
Console.WriteLine();
Console.ReadLine();
}
}
NATIJA:


TEZ TARTIBLASH USULI BILAN SARALASH




Tez tartiblash dasturi c# tilida:
using System;

class Program {


static void Main(string[] args) {
int[] arr = { 5, 3, 8, 4, 2, 1, 9, 7, 6 };
Console.WriteLine("Before sorting:");
PrintArray(arr);
QuickSort(arr, 0, arr.Length - 1);
Console.WriteLine("After sorting:");
PrintArray(arr);
}

static void QuickSort(int[] arr, int left, int right) {


if (left < right) {
int pivotIndex = Partition(arr, left, right);
QuickSort(arr, left, pivotIndex - 1);
QuickSort(arr, pivotIndex + 1, right);
}
}

static int Partition(int[] arr, int left, int right) {


int pivot = arr[right];
int i = left - 1;
for (int j = left; j < right; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp2 = arr[i + 1];
arr[i + 1] = arr[right];
arr[right] = temp2;
return i + 1;
}

static void PrintArray(int[] arr) {


foreach (int i in arr)
Console.Write(i + " ");
Console.WriteLine();
Console.ReadLine();
}
}
NATIJA:


BIRLASHTIRISH USULI BILAN SARALASH




Birlashtirish dasturi c# tilida:
using System;

class Program


{
static void Main(string[] args)
{
int n = 0;
int[] arr;
int temp;

Console.Write("n ni kiriting: ");


n = int.Parse(Console.ReadLine());

arr = new int[n];



for (int i = 0; i < n; i++)
{
Console.Write((i + 1) + "-elementni kiriting: ");
arr[i] = int.Parse(Console.ReadLine());
}

for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

Console.Write("Saralangan ketma-ketlik: ");

{
Console.Write("4 9 7 5 ");
}

Console.ReadLine();


}
}
NATIJA:



O’qituvchi: A Mirzaqulov Bajardi: Usmanjonova O



Download 193.57 Kb.

Do'stlaringiz bilan baham:
1   2




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