编写程序,实现三种排序算法(选择、插入、冒泡)

2024-12-28 04:41:40
推荐回答(1个)
回答1:

选择排序如下:
#include
#include

void choose_sort( int array[], int s, int e );

int main( void )
{
int array[100];
int num;
int i;
int j;
int k;

printf( "input the amout(<100) of the array:" );
scanf( "%d", &num );

for( i = 0; i < num; i++)
{
scanf( "%d", array + i );
}
choose_sort( array, 0, num - 1 );
/*
for( i = 0; i < num - 1; i++ )
{
k = i;
for( j = i + 1; j < num; j++ )
{
k = array[j] < array[k] ? j : k;
}
if( i != k )
{
array[i] ^= array[k];
array[k] ^= array[i];
array[i] ^= array[k];
}
}
*/

printf( "the result of choose sort:\n" );
for( i = 0; i < num; i++)
{
printf( "%d ", array[i] );
}
printf( "\n" );

system( "pause" );
return 0;
}

void choose_sort( int array[], int s, int e )
{
int i;
int j;
int k;

for( i = s; i < e; i++ )
{
k = i;
for( j = i + 1; j <= e; j++ )
{
k = array[k] > array[j] ? j : k;
}
if( k != i )
{
array[i] ^= array[k];
array[k] ^= array[i];
array[i] ^= array[k];
}
}
}

插入排序如下:
#include
#include

/*/already check */
void insert_sort( int array[], int s, int e );

int main( void )
{
int array[100];
int num;
int i;
int key;
int j;

printf( "input the amount(<100) of array: " );
scanf( "%d", &num );

for( i = 0; i < num; i++ )
{
scanf( "%d", array + i );
}

insert_sort( array, 0, num - 1 );
/*
for( i = 1; i < num; i++ )
{
key = array[i];
j = i - 1;
while( j >= 0 && array[j] > key )
{
array[j + 1] = array[j];
j--;
}
array[j + 1] = key;
}
*/
printf( "insert sort result:\n" );
for( i = 0; i < num; i++ )
{
printf( "%d ", array[i] );
}
printf( "\n" );

system( "pause" );
return 0;
}

void insert_sort( int array[], int s, int e )
{
int i;
int j;
int key;

for( i = s + 1; i <= e; i++)
{
key = array[i];
for( j = i - 1; j >= s && array[j] > key; j-- )
{
array[j + 1] = array[j];
}
array[j + 1] = key;
}
}
冒泡排序如下:
#include
#include

void bubble_sort( int array[], int s, int e );

int main( void )
{
int i;
int num;
int array[100];

printf( "input amount(<100) of number: " );
scanf( "%d", &num );
for( i = 0; i < num; i++ )
{
scanf( "%d", array + i );
}

bubble_sort( array, 0, num - 1 );

printf( "result of bubble sort:\n" );
for( i = 0; i < num; i++ )
{
printf( "%d ", array[i] );
}
printf( "\n" );

system( "pause" );
return 0;
}
/*
void bubble_sort( int array[], int s, int e )
{
int i;
int j;

for( i = e; i > s; i-- )
{
for( j = s + 1; j <= i; j++ )
{
if( array[j - 1] > array[j] )
{
array[j] ^= array[j - 1];
array[j - 1]^= array[j];
array[j] ^= array[j - 1];
}
}
}
}
*/
注意:以上三个算法的main函数部分是为了让你输入一些数来测试排序算法而写的,排序的具体实现另写成一个函数。

补充题:
#include
#include
#include

int search(char* str1, char* str2);

main()
{
char str1[100];
char str2[100];
int result;

printf("input first string: ");
scanf("%s", str1);
printf("input first string: ");
scanf("%s", str2);

result = search(str1, str2);
if(result < 0){
printf("str2 is not in str1.\n");
}
else{
printf("str2 is in str1, and begins at %d.\n", result);
}
system("pause");
}

int search(char* str1, char* str2){
int len1 = strlen(str1);
int len2 = strlen(str2);
int i;
int j;
int k;

i = 0;
while(i <= len1 - len2){
j = 0;
k = i;
while(j < len2 && str1[k] == str2[j]){
j++;
k++;
}
if(j == len2){
return i;
}
i++;
}
return -1;
}
注意:main也是测试用的,具体实现写成一个函数