Amaliy bo’lmagan saralash
Bogosort – algoritm murakkabligi O(n n!).
O’rinlashtirish saralash – algoritm murakkabligi O(n n!).
Ma’nosiz saralash (Stupid sort) – algoritm murakkabligi O(n3).
Beadasort – algoritm murakkabligi O(n) yoki O(sqrt(n)). Maxsus apparat ta’minoti talab etiladi.
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.
Do'stlaringiz bilan baham: |