Sorting and Searching Sorting Sorting is used to speed up searching in a list of objects. There are two types of sorting : Ascending and Descending. Methods of sorting : Bubble Sort Bubble sort compares a variable with their neighbour and swap them if necessary. Example : void bubbleSort(int arr[], int n){ int i, j, temp; for(i = 1; i < n; i++){ for(j = n - 1; j >= i; j--){ if(arr[j-1] > arr[j]){ temp = arr[j-1]; arr[j-1] = arr[j]; arr[j] = arr[j-1]; } } } Selection Sort for(i = 0; i < n - 1; i++){ temp = i; for(j = i+1; j < n; j++...
Posts
Showing posts from December, 2018
- Get link
- X
- Other Apps
Structure and file processing Structure Structure is a data type used to store a group of data. It is user defined and the group of data can be varied. Structure declaration : struct name_structure{ data_type name ... }; Example : struct mahasiswa{ int NIM; char nama[21]; char jurusan[51]; }agus; To call an element of a structure, use dot operator : agus.NIM Structure can also be used in conjunction with array. To declare structrure with array, just declare it as usual. struct mahasiswa{ int NIM; char nama[21]; char jurusan[51]; }data[20]; Example of storing structured array: void storeStruct(){ struct mahasiswa{ int NIM; char nama[21]; char jurusan[51]; }data[20]; int i; for(i = 0; i < 20; i++){ ...
- Get link
- X
- Other Apps
Function and Recursion Function Function in C is related to modular programming. To implement modules in C, we use function to call sub-program of a group of statements when necessary. In C, there are two types of function. First one is library function(printf, scanf) and the second one is user-defined function(self-defined). The construct of user-defined function is : return-value-type function name(parameter-list){ statements}; Example: int even(int x){ if(x % 2 == 0){ return 1; } else{ return 0; } }; int main(){ #include<stdio.h> int x; scanf("%d", &x); even(x); if(even(x) == 1){ printf("It's even\n"); } else{ printf("It's not even\n); ...