We are still actively working on the spam issue.
Difference between revisions of "Programming concepts"
(→Bubble Sort) |
(→Bubble Sort: still needs work formatting, a little less ugly now) |
||
Line 10: | Line 10: | ||
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. | 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. | ||
− | <code> | + | :<code>//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 | |
− | + | :::}//end inner for | |
− | + | : | |
− | + | ::}//end outer for | |
− | + | : | |
− | + | :://print array | |
− | + | ::for(i=0; i<SIZE; i++) | |
− | + | :::printf("%4d", a[i]); | |
− | + | ::return 0; | |
− | + | :}</code> | |
− | |||
− | </code> | ||
===Performance=== | ===Performance=== |
Revision as of 03:55, 28 January 2014
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;
}