Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第十次作业 #186

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions 2017-1/Orange33/第九次作业/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "sort.h"
int main()
{
int compare = 0, move = 0;
int length = rand()%30+1;
int *origin = (int*)malloc(length * sizeof(int));
int *arr = (int*)malloc(length * sizeof(int));

printf("����������ݣ�\n");
input(origin, length);
output(origin, length);
printf("\n");

printf("ֱ�Ӳ�������\n�����");
originArray(arr, origin, length);
InsertSort(arr, length, &compare, &move);
output(arr, length);
outTime(compare, move);

printf("ϣ������\n�����");
originArray(arr, origin, length);
ShellSort(arr, length, &compare, &move);
output(arr, length);
outTime(compare, move);

printf("��������\n�����");
originArray(arr, origin, length);
BubbleSort(arr, length, &compare, &move);
output(arr, length);
outTime(compare, move);

compare = 0, move = 0;
printf("��������\n�����");
originArray(arr, origin, length);
int left = 0, count = 0;
QuickSort(arr,left,length-1,count, &compare, &move);
output(arr, length);
outTime(compare, move);

printf("��ѡ������\n�����");
originArray(arr, origin, length);
SelectSort(arr, length, &compare, &move);
output(arr, length);
outTime(compare, move);
}
182 changes: 182 additions & 0 deletions 2017-1/Orange33/第九次作业/sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
#include "sort.h"

//ֱ�Ӳ�������
void InsertSort(int *array, int n, int *comp, int *mov)
{
*comp = *mov = 0;
int i, j;
int tmp; // ������Ԫ��
for (i = 0; i < n; i++)
{
tmp = array[i];
for (j = i - 1; j >= 0 && tmp < array[j]; j--)
{
int arr = array[j + 1];
array[j + 1] = array[j];
array[j] = arr;
(*mov) += 3;
++(*comp);
}
array[j + 1] = tmp;
}
}

//ϣ������
void ShellSort(int *array, int n, int *comp, int *mov)
{
*comp = *mov = 0;
int i, j;
int d = n / 2;
int temp;
while (d >= 1)
{
for (i = d; i < n; i++)
{
temp = array[i];
for (j = i - d; j >= 0 && temp < array[j]; j = j - d)
{
int arr = array[j + d];
array[j + d] = array[j];
array[j] = arr;
(*mov) += 3;
++(*comp);
}
if (array[j + d] != temp)
{
int arr = array[j + d];
array[j + d] = temp;
temp = arr;
(*mov) += 3;
++(*comp);
}
}
d /= 2;
}
}

//��������
void BubbleSort(int *array, int n, int *comp, int *mov)
{
*comp = *mov = 0;
int i, j;
int flag = 1; //���ѭ�������Ƿ���й����������Ϊ1������˽���
for (i = 0; i < n && flag; i++)
{
flag = 0;
for (j = 1; j < n - i; j++)
{
if (array[j - 1] > array[j])
{
int arr = array[j];
array[j] = array[j - 1];
array[j - 1] = arr;

(*mov) += 3;
flag = 1;
}
++(*comp);
}
}
}

//��������
void QuickSort(int *array, int left, int right, int count, int *comp, int *mov)
{ //left �� count��ʼֵΪ0 right ��ʼֵΪ���鳤�� - 1
int i;
int count_temp = count + 1;
if (left < right)
{
int base = array[left];
while (left < right)
{
while (left < right && base < array[right])
{
right--;
(*comp)++;
}

array[left] = array[right];
(*mov)++;

while (left < right && array[left] < base)
{
left++;
(*comp)++;
}
array[right] = array[left];
(*mov)++;
}

array[left] = base;
i = left;
QuickSort(array, left, i - 1, count_temp, comp, mov);
QuickSort(array, i + 1, right, count_temp, comp, mov);
}
}

//��ѡ������
void SelectSort(int * array, int n, int *comp, int *mov)
{
*comp = *mov = 0;
int i, j, k;
for (i = 0; i < n; i++)
{
k = i;
for (j = i + 1; j < n; j++)
{
if (array[j] < array[k])
{
k = j;
(*comp)++;
}
}
if (i != k)
{
int arr = array[i];
array[i] = array[k];
array[k] = arr;
(*mov) += 3;
}
}
}


//������ɲ�������
void input(int *a, int n)
{
srand((unsigned)time(NULL));
for (int i = 0; i < n; ++i)
{
a[i] = rand() % 100 + 1;
}
}

//��ӡ�����������
void output(int* a, int n)
{
for (int i = 0; i < n; ++i)
{
printf("%d ", a[i]);
}
printf("\n");

}

//�������
void outTime(int comp, int mov)
{
printf("�Ƚϴ���: %d\n", comp);
printf("�ƶ�����: %d\n", mov);
printf("����֮��: %d\n", comp + mov);
comp = mov = 0;
printf("\n");
}

//��ʼ���鱣��
void originArray(int* array, int* origin, int n)
{
for (int i = 0; i < n; ++i)
{
array[i] = origin[i];
}
}
16 changes: 16 additions & 0 deletions 2017-1/Orange33/第九次作业/sort.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once
#include<stdio.h>
#include <stdlib.h>
#include<time.h>


void InsertSort(int *array, int n, int *comp, int *mov); //ֱ�Ӳ�������
void ShellSort(int *array, int n, int *comp, int *mov); //ϣ������
void BubbleSort(int *array, int n, int *comp, int *mov); //��������
void QuickSort(int *array, int left, int right, int count, int *comp, int *mov);//��������
void SelectSort(int * array, int n, int *comp, int *mov);//��ѡ������
void input(int *a, int n); //������ɲ�������
void output(int *a, int n ); //��ӡ�����������
void outTime(int comp, int mov); //�������
void originArray(int* dest, int* source, int n);//��ʼ���鱣��

Loading