We are still actively working on the spam issue.
Programming concepts
This article needs to be improved.
Contents
Fundamentals are important, so here are some.
Sorting Algorithms
A big topic in computer applications is the quick and efficient sorting of data. There are many different sorting techniques, as a programmer one has to balance the algorithm's ease of reading vs the efficiency of said code. Many standard libraries/APIs already have sorting functions already written, however it is still good practice to know some of the theory behind how the different algorithms work.
Bubble Sort
The bubble sort is a very simple to read algorithm that is the standard first sorting method that most programmers learn. The bubble sort gets its name because values are swapped among each other, gradually moving to the top of the list.
//bubble sort example
#include <stdio.h>
#define SIZE 10
- int main( void ){
- int a[SIZE] = {2,6,9,1,4,15,7,2}; //array to be sorted
- int count; //to be used to count passes
- int i; //comparisons counter
- int hold; //temp variable to hold numbers
-
- //start of bubble sort
- for(count=0; count<SIZE-1; count++){//loop to control number of passes
- //size is decreased by 1 to prevent array out of bounds error
-
- for(i=0; i<SIZE-1; i++){//comparison loop
- if(a[i]>a[i+1]){//if a[i] bigger than a[i+1], swap values
- hold = a[i];
- a[i] = a[i+1];
- a[i+1] = hold;
- }//end if
- if(a[i]>a[i+1]){//if a[i] bigger than a[i+1], swap values
- }//end inner for
- for(i=0; i<SIZE-1; i++){//comparison loop
-
- }//end outer for
-
- //print array
- for(i=0; i<SIZE; i++)
- printf("%4d", a[i]);
- return 0;
}
Selection Sort
A bit further up on the food chain is the selection sort. The selection sort goes through each element position and finds the value which should occupy the next position in the sorted array. When it finds the appropriate element, the algorithm exchanges it with the valuewhich previously occupied the desired position. On the second pass it looks for the second smallest element and swaps it with the second position and so on.
//Selection sort example
- // finds smallest element between left hand and right hand
- // moves element to correct position
- include <stdio.h>
- void selectionSort(int array[], int n){
- //n is length
- int lh, rh, i, temp;
- for (lh=0; lh < n; lh++){//everything left of lh is considered sorted
- rh = lh; //reset right hand
- //runs through lh-rh bounds of array
- for (i = lh + 1; i<n; i++){
- //if lowest element found, copy index to right hand
- if (array[i] < array[rh]) rh = i;
- }
- //move lowest element to correct position
- temp = array[lh];
- array[lh] = array[rh];
- array[rh] = temp;
- }
- }
- int main( void ){
- int len = 8;
- int a[8] = {2,6,9,1,4,15,7,2}; //array to be sorted
- int i;
-
- selectionSort(a, len);
-
- //print array
- for(i=0; i<len; i++)
- printf("%4d", a[i]);
- return 0;
}
Merge sort
Performance
Search Algorithms
Two of the main searching algorithms are Knuth-Morris-Pratt (KMP) and the Boyer-Moore (BM) algorithms.
Recursion
Recursion is the concept of something entering inside itself, or other a new instance of itself. Functional programming languages such as Lisp dialects are known to employ this often.
Recursion can be tough to get your head around at first, because for a lot of people it is a new concept. However, once understood, it's quite simple. The main things to remember are the base case, the terminating case, and the concept of an accumulator.
Base case: This is the starting step of recursion.
Termination case: This tells the recursion when to stop. Without this, it would simply recur forever, or at least until the machine runs out of memory.
Accumulator: This is how you can pass data through a function recursively. It's called an accumulator because it accumulates data, or collects it as it goes. Here's an example of a program in Python that recursively adds numbers from 0 to 10 into a list, and then returns it.
The base case will be starting at 0. The terminating case is at 10. We'll use a list as an accumulator.
Lets define our function:
def one_to_ten(number, accumulator): if number > 10: return accumulator else: accumulator.append(number) return one_to_ten(number + 1, accumulator) empty_list = [] base_case = 0 result = one_to_ten(base_case, empty_list) print(result) # result is [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Going through it step by step:
1. Our function takes a number and the accumulator 2. Check that the termination case hasn't been met 3. If it has, don't call the function anymore. Just return the accumulator. 4. Otherwise, put the number in the accumulator 5. Call the function from within itself, but with number + 1, so the next recursion of it will have the next number
So, the function enters another instance of itself, but with number plus one, and it does this 10 times, until number is 10, at 10 levels deep. However, when number becomes 11, it will not recur anymore, and it will return the accumulator back to the 9th function, which will also finish and return to the 8th function, which returns back to the 7th function, and so on until the first call of the function has returned. You could say it naturally unrolls itself.