/*
** Program features: mainly use the basic operators,
** data structures, some basic operations.
**
**
*/
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
//N is the size of the program in the array
#define N 10
//The core of the merge sort algorithm
//sort array a from left to right within the range of values
void my_merge(int *a, int left, int right)
{
int start1 = left;
int middle = (left + right) / 2;
int start2 = middle + 1;
int tmp = 0;
int arr[right - left + 1];
int i;
//find out the smallest numbers to a new array for temporary storage
while(start1 <= middle && start2 <= right)
{
if(a[start1] <= a[start2])
{
arr[tmp++] = a[start1++];
}
else
{
arr[tmp++] = a[start2++];
}
}
//If there is a surplus in left part
while(start1 <= middle)
{
arr[tmp++] = a[start1++];
}
//If there is a surplus in rignt part
while(start2 <= right)
{
arr[tmp++] = a[start2++];
}
int templeft = left;
for(i = 0; i < (right - left + 1);i++)
{
a[templeft] = arr[i];
templeft++;
}
}
//Sorting algorithm, the problem is decomposed and evaluated
//Here is a recursive procedure mainly on task decomposition
void my_sort(int *v,int left,int right)
{
int middle;
if(left < right)
{
middle = (left + right)/2;
my_sort(v,left,middle);
my_sort(v,middle+1,right);
my_merge(v,left,right);
}
}
//
int my_find_large(int *v, int length)
{
int i;
int largest;
largest = v[0];
for(i = 1; i < length; i++)
{
if( largest < v[i])
{
//Here With XOR variable exchange
v[i] = v[i] ^largest;
largest = v[i] ^largest;
v[i] = v[i] ^ largest;
}
}
return largest;
}
//
int my_find_small(int *v, int length)
{
int i;
int smallest;
smallest = v[0];
for(i = 1; i < length; i++)
{
if( smallest > v[i])
{
//Here With XOR variable exchange
v[i] = v[i] ^smallest;
smallest = v[i] ^smallest;
v[i] = v[i] ^ smallest;
}
}
return smallest;
}
//bubble sort
bubble_sort(int *a,int left,int right)
{
int i,j,k;
for(i=left;i<right;i++)
{
for(j=i;j<right;j++)
{
if(a[j] > a[i])
{
a[j] = a[j]^a[i];
a[i] = a[i]^a[j];
a[j] = a[i]^a[j];
}
}
}
}
//read function to filter out the wrong input
//Enter a number to identify, enter cls will clear the screen, typing exit will quit
int read()
{
char a[10];
int back = 0,length,i=0;
scanf("%s",a);
length = strlen(a);
for(i=0;i<length;i++)
{
if(isdigit(a[i])==0) break;
}
if(i==length) //number
{
int pow = 10,j;
for(j=length-1;j>=0;j--)
{
if(j==length-1) back = (a[j]-'0');
else
{
back += ((a[j]-'0')*pow);
pow = pow *10;
}
}
}
else //wrong input
{
if(strcmp(a,"cls")==0)
{
system("cls");
}
else if(strcmp(a,"exit")==0)
{
exit(0);
}
else
{
printf("Input errors, please re-enter\n");
}
}
return back;
}
int main()
{
int a[N];
int i,j;
int largest,smallest;
int input;
srand((int)time(0)); //Initialize the random number generator
for(i = 0;i < N;i++)
{
a[i] = ((rand()%30)+1); //Array initialization
}
printf("Look the number \n");//Sort before output
for(i = 0;i < N;i++)
{
for(j = 0;j < a[i];j++)
{
printf("*");
}
printf("\n");
}
largest = my_find_large(a,N);//Find out the largest number
printf("Below is the largest number\n");
for(i = 0;i < largest;i++)
{
printf("*");
}
printf("\n");
smallest = my_find_small(a,N);//Find out the smallest number
printf("Below is the smallest number\n");
for(i = 0;i < smallest;i++)
{
printf("*");
}
printf("\n");
bubble_sort(a,0,N); //The numbers in the array are sorted in ascending order
printf("Look the number after the bubble sort big to little\n");
for(i = 0;i < N;i++)
{
for(j = 0;j < a[i];j++)
{
printf("*");
}
printf("\n");
}
my_sort(a,0,N-1); //The numbers in the array are sorted in ascending order
printf("Look the number after the Merge sort little to big\n");
for(i = 0;i < N;i++)
{
for(j = 0;j < a[i];j++)
{
printf("*");
}
printf("\n");
}
printf("\n\nEnter your numbers, enter Exit to exit\n");
while(1)
{
input = read();
for(i = 0; i < input; i++)
{
printf("*");
}
printf("\n");
}
return 0;
}
** Program features: mainly use the basic operators,
** data structures, some basic operations.
**
**
*/
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
//N is the size of the program in the array
#define N 10
//The core of the merge sort algorithm
//sort array a from left to right within the range of values
void my_merge(int *a, int left, int right)
{
int start1 = left;
int middle = (left + right) / 2;
int start2 = middle + 1;
int tmp = 0;
int arr[right - left + 1];
int i;
//find out the smallest numbers to a new array for temporary storage
while(start1 <= middle && start2 <= right)
{
if(a[start1] <= a[start2])
{
arr[tmp++] = a[start1++];
}
else
{
arr[tmp++] = a[start2++];
}
}
//If there is a surplus in left part
while(start1 <= middle)
{
arr[tmp++] = a[start1++];
}
//If there is a surplus in rignt part
while(start2 <= right)
{
arr[tmp++] = a[start2++];
}
int templeft = left;
for(i = 0; i < (right - left + 1);i++)
{
a[templeft] = arr[i];
templeft++;
}
}
//Sorting algorithm, the problem is decomposed and evaluated
//Here is a recursive procedure mainly on task decomposition
void my_sort(int *v,int left,int right)
{
int middle;
if(left < right)
{
middle = (left + right)/2;
my_sort(v,left,middle);
my_sort(v,middle+1,right);
my_merge(v,left,right);
}
}
//
int my_find_large(int *v, int length)
{
int i;
int largest;
largest = v[0];
for(i = 1; i < length; i++)
{
if( largest < v[i])
{
//Here With XOR variable exchange
v[i] = v[i] ^largest;
largest = v[i] ^largest;
v[i] = v[i] ^ largest;
}
}
return largest;
}
//
int my_find_small(int *v, int length)
{
int i;
int smallest;
smallest = v[0];
for(i = 1; i < length; i++)
{
if( smallest > v[i])
{
//Here With XOR variable exchange
v[i] = v[i] ^smallest;
smallest = v[i] ^smallest;
v[i] = v[i] ^ smallest;
}
}
return smallest;
}
//bubble sort
bubble_sort(int *a,int left,int right)
{
int i,j,k;
for(i=left;i<right;i++)
{
for(j=i;j<right;j++)
{
if(a[j] > a[i])
{
a[j] = a[j]^a[i];
a[i] = a[i]^a[j];
a[j] = a[i]^a[j];
}
}
}
}
//read function to filter out the wrong input
//Enter a number to identify, enter cls will clear the screen, typing exit will quit
int read()
{
char a[10];
int back = 0,length,i=0;
scanf("%s",a);
length = strlen(a);
for(i=0;i<length;i++)
{
if(isdigit(a[i])==0) break;
}
if(i==length) //number
{
int pow = 10,j;
for(j=length-1;j>=0;j--)
{
if(j==length-1) back = (a[j]-'0');
else
{
back += ((a[j]-'0')*pow);
pow = pow *10;
}
}
}
else //wrong input
{
if(strcmp(a,"cls")==0)
{
system("cls");
}
else if(strcmp(a,"exit")==0)
{
exit(0);
}
else
{
printf("Input errors, please re-enter\n");
}
}
return back;
}
int main()
{
int a[N];
int i,j;
int largest,smallest;
int input;
srand((int)time(0)); //Initialize the random number generator
for(i = 0;i < N;i++)
{
a[i] = ((rand()%30)+1); //Array initialization
}
printf("Look the number \n");//Sort before output
for(i = 0;i < N;i++)
{
for(j = 0;j < a[i];j++)
{
printf("*");
}
printf("\n");
}
largest = my_find_large(a,N);//Find out the largest number
printf("Below is the largest number\n");
for(i = 0;i < largest;i++)
{
printf("*");
}
printf("\n");
smallest = my_find_small(a,N);//Find out the smallest number
printf("Below is the smallest number\n");
for(i = 0;i < smallest;i++)
{
printf("*");
}
printf("\n");
bubble_sort(a,0,N); //The numbers in the array are sorted in ascending order
printf("Look the number after the bubble sort big to little\n");
for(i = 0;i < N;i++)
{
for(j = 0;j < a[i];j++)
{
printf("*");
}
printf("\n");
}
my_sort(a,0,N-1); //The numbers in the array are sorted in ascending order
printf("Look the number after the Merge sort little to big\n");
for(i = 0;i < N;i++)
{
for(j = 0;j < a[i];j++)
{
printf("*");
}
printf("\n");
}
printf("\n\nEnter your numbers, enter Exit to exit\n");
while(1)
{
input = read();
for(i = 0; i < input; i++)
{
printf("*");
}
printf("\n");
}
return 0;
}