Application of Simple Algorithms

#include "stdio.h"
//Search by half
int searchItem(int array[], int len, int key);

int main(int argc, char const *argv[])
{
int array[] = {1,23, 34,12,2,3,45,6,7,8};
long len = sizeof(array)/sizeof(int);
int location = searchItem(array,10,8);

printf("location = %d\ n",location );
return 0;
}


/**
*Use halved search to find a number
*
*parameter 1 array array
*parameter 2 len array length
*parameter 3 key search number
*return the location of the search number, if search Not return -1
*/

int searchItem(int array[], int len, int key){

//1. First define variables
int low = 0, high = len - 1, mid;
//2. Loop to find
while( low <= high){

//2.1 Calculate mid position
mid = (low + high)/2;

// Judge the size of key array[mid], and find in that half area
if (key > array[mid])
{//Right half area
low = mid + 1;
}else if (key < array[mid])
{
high = mid - 1;
}else{

return mid;
}
}


return -1;
}

 

 

 

#include "stdio.h"

void selection_sort(int array[], int len);//Selection sort

int main(int argc, char const *argv[])
{
int a[10] = {1,200,290,12,23, 12,45,67,78,90};

//1. Print out the element order before
sorting printf("Before sorting:\n");
for (int i = 0; i < 10; ++i)
{
printf ("%d\t",a[i]);
}
printf("\n");

//2. Sort
selection_sort(a,10);//Pass the array name and length of the array

//3. Print out the order of elements after
sorting printf("After sorting:\n");
for (int i = 0; i < 10; ++i)
{
printf("%d\t",a[i]);
}
printf("\n");

return 0;
}
/*
Description:
select sort
parameters: array name and array length
Return value: none

*/

void selection_sort(int array[], int len)
{
int temp;
// double loop
for (int i = 0; i < len - 1; ++i)
{
for (int j = i + 1; j < len; ++j)
{
// swap
if there is a smaller number if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j ] = temp;
}
}
}
}

 

 

 

#include "stdio.h"

void bubble_sort(int array[], int len);

int main(int argc, char const *argv[])
{
int a[10] = {1,200,290,12,23,12,45, 67,78,90};

//1. Print out the element order before
sorting printf("Before sorting:\n");
for (int i = 0; i < 10; ++i)
{
printf("%d \t",a[i]);
}
printf("\n");

//2. Sort
bubble_sort(a,10);//Pass the array name and the length of the array

//3. Print the output after sorting Element order of
printf("After sorting:\n");
for (int i = 0; i < 10; ++i)
{
printf("%d\t",a[i]);
}
printf("\ n");

return 0;
}

/*
Bubble sort function

parameters: array and the length of the array
Return value: None
*/
void bubble_sort(int array[], int len)
{

//Because every time the value of i changes, it runs a trip. According to the idea of ​​bubbling, there is already a number sinking
//There is no need to compare the
int temp with the number that has sinking;
//1. Controlling The number of times
for (int i = 0; i < len - 1; ++i)
{
//2. Compare from the first one, if it is greater than it, exchange it, otherwise do not exchange
for (int j = 0; j < len - 1 - i ; ++j)
{

if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324740203&siteId=291194637