[면접] 일부 정렬 알고리즘 코드

3438 단어
직접 삽입 정렬
#include <stdio.h>
void InsertSort(int data[],int num)
{
	int temp = 0;
	for(int i = 1  ; i < num ; i++)
	{
		if(data[i] < data[i-1])
		{
			temp = data[i];
			for(int j = i-1 ; j >= 0 ; j--)
			{
				if(temp < data[j])
					data[j+1] = data[j];
				else
					break;
			}
			data[j+1] = temp;
		}
	}
}
void main() 
{
	int a[8] = {3,5,45,35,12,95,55,8};
	InsertSort(a,8);
	for(int i = 0 ; i < 8 ; i++)
		printf("%d ",a[i]);
}

단순 선택 정렬
첫 번째:
void SelectSort(int data[],int num)
{
	int temp = 0;
	for(int i = 0 ; i < num-1 ; i++)
	{
		for(int j = i+1 ; j < num ; j++)
		{
			if(data[j] < data[i])
			{
				temp = data[j];
				data[j] = data[i];
				data[i] = temp;
			}
		}
	}
}

두 번째:
void SelectSort(int data[],int num)
{
	int temp = 0 , pos = 0;
	for(int i = 0 ; i < num-1 ; i++)
	{
		pos = i;
		for(int j = i+1 ; j < num ; j++)
		{
			if(data[j] < data[pos])
			{
				pos = j;
			}
		}
		temp = data[pos];
		data[pos] = data[i];
		data[i] = temp;
	}
}

거품 정렬
void BuddleSort(int data[],int num)
{
	int temp = 0 , flag = 0;
	for(int i = 0 ; i < num-1 ; i++)
	{
		flag = 0;
		for(int j = 0 ; j < num-1-i ; j++)
		{
			if(data[j] > data[j+1])
			{
				temp = data[j];
				data[j] = data[j+1];
				data[j+1] = temp;
				flag = 1;
			}
		}
		if(!flag)
			break;
	}
}

빠른 정렬
void swap(int &a,int &b)
{
	int temp = 0;
	temp = a;
	a = b;
	b = temp;
}
void QuickSort(int data[],int left,int right)
{
	if(left < right)
	{
		int i = left , j = right;
		while(i < j)
		{
			while(data[j] > data[left])
				j--;
			while(data[i] < data[left])
				i++;
			swap(data[i] , data[j]);
		}
		swap(data[left] , data[j]);
		QuickSort(data , left,j-1);
		QuickSort(data , j+1 , right);
	}
}

병합 정렬
void Merge(int data[],int left,int center,int right)
{
	int *temp = new int[right-left+1];
	int i = left , j = center+1 , k = 0;
	while(i <= center && j <= right)
	{
		if(data[i]<data[j])
			temp[k++] = data[i++];
		else
			temp[k++] = data[j++];
	}
	while(i <= center)
		temp[k++] = data[i++];
	while(j <= right)
		temp[k++] = data[j++];
	i = left;
	j = 0;
	while(i <= right && j < k)
	{
		data[i++] = temp[j++];
	}
	delete []temp;
}
void MergeSort(int data[],int left,int right)
{
	if(left < right)
	{
		int mid = (left+right)/2;
		MergeSort(data,left,mid);
		MergeSort(data,mid+1,right);
		Merge(data,left,mid,right);
	}
}

더미 정렬
#include <stdio.h>
void swap(int &a,int &b)
{
	int temp = 0;
	temp = a;
	a = b;
	b = temp;
}
// 
void Restore(int data[],int f,int e)//f ,e 
{
	int j = f,m = 0;//m 
	while(j <= e/2)
	{
		if((2*j < e) && (data[2*j] < data[2*j+1]))
			m = 2*j+1;
		else
			m = 2*j;
		if(data[m] > data[j])// 
		{
			swap(data[m],data[j]);// , 
			j = m;
		}
		else// , 
			break;
	}

}
void HeapSort(int data[],int n)
{
	for(int i = n/2 ; i > 0 ; i--)// 
		Restore(data,i,n);
	for(i = n ; i > 1 ; i--)
	{
		swap(data[1],data[i]);
		Restore(data,1,i-1);
	}
}
void main() 
{
	int a[9] = {0,3,5,45,35,12,99,55,8};// 1 ,a[0] 
	HeapSort(a,9);
	for(int i = 1 ; i < 9 ; i++)
		printf("%d ",a[i]);
}

좋은 웹페이지 즐겨찾기