面试准备几个基础的算法
2018-03-01 16:38
387 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sort1
{
class Program
{
//选择排序
public static int[] SelectSort(int[] a)
{
int smallest;
int i;
int j;
for (i = 0; i < a.Length - 1; i++)
{
smallest = i;
for (j = i + 1; j < a.Length; j++)
{
if (a[j] < a[smallest])
{
smallest = j;
}
}
Swap(ref a[smallest], ref a[i]);
}
return a;
}
//冒泡排序
public static int[] BubbleSort(int[] a)
{
bool flag;
int m = 0;
int n = 0;
for (int i = 0; i < a.Length - 1; i++)
{
flag = true;
if (i % 2 == 0)
{
for (int j = n; j < a.Length - 1 - m; j++)
{
if (a[j] < a[j + 1])
{
Swap(ref a[j], ref a[j + 1]);
flag = false;
}
}
if (flag)
break;
m++;
}
else
{
for (int k = a.Length - 1 - m; k >n; k--)
{
if (a[k] > a[k - 1])
{
Swap(ref a[k], ref a[k - 1]);
flag = false;
}
}
if (flag)
break;
n++;
}
}
return a;
}
//插入排序
public static void InsertSort(int[] a)
{
int temp;
for (int i = 1; i < a.Length; i++)
{
temp = a[i];
for (int j = i - 1; j >= 0;j-- )
{
if (a[j] > temp)
{
a[j + 1] = a[j];
if (j == 0)
{
a[0] = temp;
break;
}
}
else
{
a[j + 1] = temp;
break;
}
}
}
}
//快速排序
public static void QuickSort(int[] a,int low,int high)
{
if (low >= high) return;
int temp = a[low];
int i=low+1;
int j=high;
while (true)
{
while (a[j] > temp) j--;
while (a[i] < temp && i < j) i++;
if (i >= j) break;
Swap(ref a[j], ref a[i]);
i++;
j--;
}
if (j != low)
{
Swap(ref a[j], ref a[low]);
}
QuickSort(a,j+1,high
4000
);
QuickSort(a, low, j - 1);
}
public static void Swap(ref int p1, ref int p2)
{
int temp;
temp = p1;
p1 = p2;
p2 = temp;
}
static void Main(string[] args)
{
int[] a = { 10, 2, 14, 4, 6,12,24,56,33,46,98,21,26,13,17,88 };
//QuickSort(a,0,a.Length-1);
InsertSort(a);
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
Console.Read();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sort1
{
class Program
{
//选择排序
public static int[] SelectSort(int[] a)
{
int smallest;
int i;
int j;
for (i = 0; i < a.Length - 1; i++)
{
smallest = i;
for (j = i + 1; j < a.Length; j++)
{
if (a[j] < a[smallest])
{
smallest = j;
}
}
Swap(ref a[smallest], ref a[i]);
}
return a;
}
//冒泡排序
public static int[] BubbleSort(int[] a)
{
bool flag;
int m = 0;
int n = 0;
for (int i = 0; i < a.Length - 1; i++)
{
flag = true;
if (i % 2 == 0)
{
for (int j = n; j < a.Length - 1 - m; j++)
{
if (a[j] < a[j + 1])
{
Swap(ref a[j], ref a[j + 1]);
flag = false;
}
}
if (flag)
break;
m++;
}
else
{
for (int k = a.Length - 1 - m; k >n; k--)
{
if (a[k] > a[k - 1])
{
Swap(ref a[k], ref a[k - 1]);
flag = false;
}
}
if (flag)
break;
n++;
}
}
return a;
}
//插入排序
public static void InsertSort(int[] a)
{
int temp;
for (int i = 1; i < a.Length; i++)
{
temp = a[i];
for (int j = i - 1; j >= 0;j-- )
{
if (a[j] > temp)
{
a[j + 1] = a[j];
if (j == 0)
{
a[0] = temp;
break;
}
}
else
{
a[j + 1] = temp;
break;
}
}
}
}
//快速排序
public static void QuickSort(int[] a,int low,int high)
{
if (low >= high) return;
int temp = a[low];
int i=low+1;
int j=high;
while (true)
{
while (a[j] > temp) j--;
while (a[i] < temp && i < j) i++;
if (i >= j) break;
Swap(ref a[j], ref a[i]);
i++;
j--;
}
if (j != low)
{
Swap(ref a[j], ref a[low]);
}
QuickSort(a,j+1,high
4000
);
QuickSort(a, low, j - 1);
}
public static void Swap(ref int p1, ref int p2)
{
int temp;
temp = p1;
p1 = p2;
p2 = temp;
}
static void Main(string[] args)
{
int[] a = { 10, 2, 14, 4, 6,12,24,56,33,46,98,21,26,13,17,88 };
//QuickSort(a,0,a.Length-1);
InsertSort(a);
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
Console.Read();
}
}
}
相关文章推荐
- 准备算法面试
- 几个Python中的基础算法
- 机器学习-算法工程师 -面试/笔试准备-重要知识点梳理
- 几个基础算法介绍和实现——插入排序
- 程序员如何快速准备面试中的算法
- 程序员如何快速准备面试中的算法
- 10个经典的C语言面试基础算法及代码
- C# 面试前的准备_基础知识点的回顾_03
- 最近在准备面试,总结了几个java中面向对象的几个问题,问题本事还不够全面,要想知道还是要自己去找,但是在面试上应该是没多大问题了
- 基础知识面试准备
- 程序员如何快速准备面试中的算法
- PHP写的几个基础算法
- 淘宝面试的几个算法题
- 准备面试需要知道的经典算法--快排
- android 面试准备基础题
- 程序员如何快速准备面试中的算法
- java 基础面试准备资料 & 面试题
- 准备Java面试之Java SE基础知识——问题篇
- 面试复习——Android工程师之算法基础
- 程序员如何快速准备面试中的算法