十大排序法
冒泡排序法
using System
;
using System
.Collections
.Generic
;
using System
.Linq
;
using System
.Text
;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
Sort(7, 3, 8, 2,9,1);
}
private static void Sort(params int[] array
)
{
int temp
= 0;
for (int i
= 0; i
< array
.Length
- 1; i
++)
{
for (int j
= 0; j
< array
.Length
- 1 - i
; j
++)
{
if (array
[j
] > array
[j
+ 1])
{
temp
= array
[j
];
array
[j
] = array
[j
+ 1];
array
[j
+ 1] = temp
;
}
}
}
foreach (int elements
in array
)
{
Console
.WriteLine(elements
);
}
Console
.ReadKey();
}
}
}
二分法排序
using System
;
using System
.Collections
.Generic
;
using System
.Linq
;
using System
.Text
;
namespace 二分法
{
class Program
{
static void Main(string[] args
)
{
int[] a
= { 1, 6, 4, 3, 8, 5, 21, 9, 31, 81, 101, 55, 62, 151, 7, 2, 10 };
DichotomySort(a
);
for (int i
= 0; i
< a
.Length
; i
++)
{
Console
.WriteLine(a
[i
]);
}
Console
.Read();
}
public static void DichotomySort(int[] array
)
{
for (int i
= 0; i
< array
.Length
; i
++)
{
int start
= 0;
int end
= i
- 1;
int middle
= 0;
int temp
= array
[i
];
while (start
<= end
)
{
middle
= (start
+ end
) / 2;
if (array
[middle
] > temp
)
{
end
= middle
- 1;
}
else
{
start
= middle
+ 1;
}
}
for (int j
= i
- 1; j
> end
; j
--)
{
array
[j
+ 1] = array
[j
];
}
array
[end
+ 1] = temp
;
}
}
}
}
二分法查找
using System
;
using System
.Collections
.Generic
;
using System
.Linq
;
using System
.Text
;
using System
.Threading
.Tasks
;
namespace 二分法
{
class Program
{
static void Main(string[] args
)
{
int[] arr
= {1,2,3,4,5,6,7,8,9,10 };
int value = BinarySearCh(arr
,8);
Console
.WriteLine(value);
Console
.ReadLine();
}
public static int BinarySearCh(int[] arr
, int value)
{
int fir
= 0;
int end
= arr
.Length
- 1;
while (fir
<= end
)
{
int num
= (fir
+ end
) / 2;
if (value == arr
[num
])
{
return num
;
}
else if (value > arr
[num
])
{
fir
= num
+ 1;
}
else
{
end
= num
- 1;
}
}
return -1;
}
}
}
拉格朗日插值法
<pre name
="code" class="cpp">void search2(int a
[M], int num
)
{
int tou
= 0;
int wei
= M
- 1;
int zhong
;
int flag
= -1;
int ci
= 0;
while (tou
<= wei
)
{
ci
++;
zhong
= tou
+ (wei
- tou
) *1.0*(num
- a
[tou
]) / (a
[wei
] - a
[tou
]);
printf("%d %d %d %d\n", tou
, wei
, zhong
, ci
);
if (num
== a
[zhong
])
{
printf("一共查找%d次 找到:a[%d]=%d\n", ci
, zhong
, num
);
flag
= 1;
break;
}
else if (num
> a
[zhong
])
{
tou
= zhong
+ 1;
}
else
{
wei
= zhong
- 1;
}
}
if (-1 == flag
)
{
printf("没有找到\n");
}
}
void main()
{
int a
[M];
for (size_t i
= 0; i
< M
; i
++)
{
a
[i
] = i
;
printf("%d ", i
);
}
printf("\n请输入要查找的数据:");
int num
;
scanf("%d", &num
);
search2(a
, num
);
system("pause");
}
快速排序法
static void Main(string[] args
)
{
Console
.WriteLine("请输入待排序数列(以\",\"分割):");
string _s
= Console
.ReadLine();
string[] _sArray
= _s
.Split(",".ToCharArray());
int _nLength
= _sArray
.Length
;
int[] _nArray
= new int[_nLength
];
for (int i
= 0; i
< _nLength
; i
++)
{
_nArray
[i
] = Convert
.ToInt32(_sArray
[i
]);
}
var list
= _nArray
.ToList();
QuickSort(list
, 0, _nLength
- 1);
foreach (var i
in list
)
{
Console
.WriteLine(i
.ToString());
}
while (true)
{
Thread
.Sleep(10000);
}
}
private static int Division(List
<int> list
, int left
, int right
)
{
while (left
< right
)
{
int num
= list
[left
];
if (num
> list
[left
+ 1])
{
list
[left
] = list
[left
+ 1];
list
[left
+ 1] = num
;
left
++;
}
else
{
int temp
= list
[right
];
list
[right
] = list
[left
+ 1];
list
[left
+ 1] = temp
;
right
--;
}
Console
.WriteLine(string.Join(",", list
));
}
Console
.WriteLine("--------------\n");
return left
;
}
private static void QuickSort(List
<int> list
, int left
, int right
)
{
if (left
< right
)
{
int i
= Division(list
, left
, right
);
QuickSort(list
, i
+ 1, right
);
QuickSort(list
, left
, i
- 1);
}
}
using System
;
using System
.Collections
.Generic
;
using System
.Linq
;
using System
.Text
;
using System
.Threading
.Tasks
;
namespace 排序法
{
class Program
{
static void Main(string[] args
)
{
int[] arr
= {5,8,3,6,7,4,9,1,2 };
QuickSort(arr
, 0, arr
.Length
- 1);
Console
.WriteLine("排序后的数列:");
foreach (int item
in arr
)
Console
.WriteLine(item
);
Console
.ReadLine();
}
private static void QuickSort(int[] arr
, int begin
, int end
)
{
if (begin
>= end
) return;
int pivotIndex
= QuickSort_Once(arr
, begin
, end
);
QuickSort(arr
, begin
, pivotIndex
- 1);
QuickSort(arr
, pivotIndex
+ 1, end
);
}
private static int QuickSort_Once(int[] arr
, int begin
, int end
)
{
int pivot
= arr
[begin
];
int i
= begin
;
int j
= end
;
while (i
< j
)
{
while (arr
[j
] >= pivot
&& i
< j
) j
--;
arr
[i
] = arr
[j
];
while (arr
[i
] <= pivot
&& i
< j
) i
++;
arr
[j
] = arr
[i
];
}
arr
[i
] = pivot
;
return i
;
}
}
}
快排
using System
;
using System
.Collections
.Generic
;
using System
.Linq
;
using System
.Text
;
using System
.Threading
.Tasks
;
namespace 快速排序
{
class Program
{
static void Main(string[] args
)
{
int[] arr
= { 5, 8, 3, 6, 7, 4, 9, 1, 2 };
quickSort(arr
, 0, arr
.Length
- 1);
Console
.WriteLine("排序后的数列:");
foreach (int item
in arr
)
{
Console
.WriteLine(item
);
}
Console
.ReadLine();
}
private static void quickSort(int[] array
, int low
, int high
)
{
if (low
> high
)
{
return;
}
int first
, last
, key
;
first
= low
;
last
= high
;
key
= array
[first
];
while (first
< last
)
{
while (first
< last
&& array
[last
] >= key
)
{
--last
;
}
array
[first
] = array
[last
];
while (first
< last
&& array
[first
] <= key
)
{
++first
;
}
array
[last
] = array
[first
];
}
array
[first
] = key
;
quickSort(array
, low
, first
- 1);
quickSort(array
, first
+ 1, high
);
}
}
}
冒泡优化,判断是否是最优顺序
static void Sort(int[] array
)
{
int tmp
= 0;
int lastExchangeIndex
= 0;
int sortBorder
= array
.Length
- 1;
for (int i
= 0; i
< array
.Length
; i
++)
{
bool isStop
= true;
for (int j
= 0; j
< sortBorder
; j
++)
{
if (array
[j
]>array
[j
+1])
{
tmp
= array
[j
];
array
[j
] = array
[j
+ 1];
array
[j
+ 1] = tmp
;
isStop
= false;
lastExchangeIndex
= j
;
}
}
sortBorder
= lastExchangeIndex
;
if(isStop
)
{
break;
}
}
}```
转载请注明原文地址: https://www.6miu.com/read-5028864.html