Amaliy matematika va informatika” yo’nalishi 18. 06-guruh talabasi Otajonova Oyzoda Sodiqjon qizining


Download 0.79 Mb.
bet7/11
Sana05.01.2022
Hajmi0.79 Mb.
#212981
1   2   3   4   5   6   7   8   9   10   11
Bog'liq
Otajonova Oyzoda Algoritmlar nazariyasi

Amaliy bo’lmagan saralash

  1. Bogosort – algoritm murakkabligi O(n n!).

  2. O’rinlashtirish saralash – algoritm murakkabligi O(n n!).

  3. Ma’nosiz saralash (Stupid sort) – algoritm murakkabligi O(n3).

  4. Beadasort – algoritm murakkabligi O(n) yoki O(sqrt(n)). Maxsus apparat ta’minoti talab etiladi.

  5. Quymoqli saralash (Pancake sorting) – algoritm murakkabligi O(n). Maxsus apparat ta’minoti talab etiladi.

Algoritmning C# dasturlash tilidagi kodi:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace saralash_2

{

public class BubbleSort

{

public static void Main(string[] args)

{

int n;

Console.Write("Elementlar sonini kirirting: n= ");

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

int[] a = new int[n];

for (int i = 0; i < n; i++)

{

a[i] = int.Parse(Console.ReadLine());

}

bubble_srt(a);

Console.ReadKey();

}

public static void bubble_srt(int[] massiv)

{

// funksiyamizga saralash uchun saralanmagan bir o`lchamli massiv kiritiladi

int n = massiv.Length;

int k;

for (int m = n; m > 0; m--)

{

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

{

k = i + 1;

if (massiv[i] > massiv[k])

{

almashtirish(i, k, massiv);//almashtirish uchun alohida funksiya

}

}

chiqarish(massiv);//chop etish uchun alohida funksiya

}

}



private static void almashtirish(int i, int j, int[] massiv)

{

int temp;

temp = massiv[i];

massiv[i] = massiv[j];

massiv[j] = temp;

}



private static void chiqarish(int[] input)

{

for (int i = 0; i < input.Length; i++)

{

Console.Write(input[i] + ", ");

}

Console.WriteLine("\n");

}

}

}

Natija:

2, 4, 6, 9, 12, 0, 1, 23, 34, // 1-qadam.

2, 4, 6, 9, 0, 1, 12, 23, 34, //2-qadam.

2, 4, 6, 0, 1, 9, 12, 23, 34,//3-qadam.

2, 4, 0, 1, 6, 9, 12, 23, 34,//4-qadam.

2, 0, 1, 4, 6, 9, 12, 23, 34,//5-qadam.

0, 1, 2, 4, 6, 9, 12, 23, 34,//6-qadam.

0, 1, 2, 4, 6, 9, 12, 23, 34, // saralangan holdagi massiv.



Download 0.79 Mb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   10   11




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