Algorithms
C
YANG HU
Simple is the beginning of wisdom. From the essence of practice,this book to briefly explain the concept and vividly cultivate programming interest, you will learn it easy and fast.
http://en.verejava.com
Copyright 2020 Yang Hu
All rights reserved.
ISBN : 979867399392 7
CONTENTS
.
.
.
.
.
14.1
14.2
14.3
14.4
15.1
15.2
15.3
15.4
16.1
16.2
16.3
17.1
17.2
17.3
23.1
23.2
23.3
23.4
23.5
23.6
26.1
26.2
26.3
.
.
.
.
.
.
.
Linear Table Definition
Linear Table:
Sequence of elements, is a one-dimensional array.
. Define a one-dimensional array of student scores
TestOneArray.c
#include int main () { int scores [] = { , , , , , }; int length = sizeof ( scores ) / sizeof ( scores [ ]); int i ; for ( i = ; i < length ; i ++) { printf ( "%d," , scores [ i ]); } return ; } |
Result:
90,70,50,80,60,85,
Maximum Value
Maximum of Integer Sequences:
. Algorithmic ideas
Compare arrays[i] with arrays[i + 1] , if arrays[i] > arrays[i + 1] are exchanged. So continue until the last number, arrays[length - 1] is the maximum.
TestMaxValue.c
#include int max ( int arrays [], int length ) { int i ; for ( i = ; i < length - ; i ++) { if ( arrays [ i ] > arrays [ i + ]) // swap { int temp = arrays [ i ]; arrays [ i ] = arrays [ i + ]; arrays [ i + ] = temp ; } } int maxValue = arrays [ length - ]; return maxValue ; } int main () { int scores [] = { , , , , }; int length = sizeof ( scores ) / sizeof ( scores [ ]); int maxValue = max ( scores , length ); printf ( "Max Value = %d\n" , maxValue ); } |
Result:
Max Value = 95
Bubble Sorting Algorithm
Bubble Sorting Algorithm:
Compare arrays[j] with arrays[j + 1] , if arrays[j] > arrays[j + 1] are exchanged.
Remaining elements repeat this process, until sorting is completed.
Sort the following numbers from small to large
Explanation:
No sorting,
Comparing,
Already sorted
. First sorting:
. Second sorting:
. Third sorting:
No swap so terminate sorting : we can get the sorting numbers from small to large
TestBubbleSort.c
#include int main () { int scores [] = { , , , , , }; int length = sizeof ( scores ) / sizeof ( scores [ ]); sort ( scores , length ); int i ; for ( i = ; i < length ; i ++) { printf ( "%d," , scores [ i ]); } return ; } void sort ( int arrays [], int length ) { int i ; int j ; for ( i = ; i < length - ; i ++) { for ( j = ; j < length - i - ; j ++) { //swap if ( arrays [ j ] > arrays [ j + ]) { int flag = arrays [ j ]; arrays [ j ] = arrays [ j + ]; arrays [ j + ] = flag ; } } } } |
Result:
50,60,70,80,85,90,
Minimum Value
Search the Minimum of Integer Sequences:
. Algorithmic ideas
Initial value minIndex=0, j=1 Compare arrays[minIndex] with arrays[j]
if arrays[minIndex] > arrays[j] then minIndex=j, j++ else j++. continue until the last number, arrays[minIndex] is the Min Value.
TestMinValue.c
#include int min ( int arrays [], int length ) { int minIndex = ; // the index of the minimum int j ; for ( j = ; j < length ; j ++) { if ( arrays [ minIndex ] > arrays [ j ]) { minIndex = j ; } } return arrays [ minIndex ]; } int main () { int scores [] = { , , , , }; int length = sizeof ( scores ) / sizeof ( scores [ ]); int minValue = min ( scores , length ); printf ( "Min Value = %d\n" , minValue ); } |
Result:
Min Value = 50
Select Sorting Algorithm
Select Sorting Algorithm:
Sorts an array by repeatedly finding the minimum element from unsorted part and putting it at the beginning.
Sort the following numbers from small to large
Explanation:
No sorting,