冒泡排序(C#数据结构学习九)
using System;
using System.Collections.Generic;
using System.Text;
namespace SoloDataStructure
{
class MyBubbleSort
{
//冒泡排序
static void BubbleSort(int[] arr)
{
int n = arr.Length;
for (int i = 0; i < n - 1; i++) //做n-1趟排序
{
bool noswap = true; //未交换标志
for (int j = n - 1; j > i; j--)
{
if (arr[j] < arr[j-1])
{
//swap记录
int temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
noswap = false;
}
}
if (noswap)
return;
}
}
static void Main(string[] args)
{
int[] arr = new int[] {99,88, 4,9,2,77,32,11,46};
BubbleSort(arr);
Console.Write("Data After BubbleSort:");
foreach (int i in arr)
{
Console.Write(i+",");
}
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace SoloDataStructure
{
class MyBubbleSort
{
//冒泡排序
static void BubbleSort(int[] arr)
{
int n = arr.Length;
for (int i = 0; i < n - 1; i++) //做n-1趟排序
{
bool noswap = true; //未交换标志
for (int j = n - 1; j > i; j--)
{
if (arr[j] < arr[j-1])
{
//swap记录
int temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
noswap = false;
}
}
if (noswap)
return;
}
}
static void Main(string[] args)
{
int[] arr = new int[] {99,88, 4,9,2,77,32,11,46};
BubbleSort(arr);
Console.Write("Data After BubbleSort:");
foreach (int i in arr)
{
Console.Write(i+",");
}
Console.ReadLine();
}
}
}