Click here to load reader
Upload
rasan-samarasinghe
View
141
Download
4
Embed Size (px)
DESCRIPTION
Sorting Algorithms for Certificate in C/C++ Programming - ESOFT Metro Campus
Citation preview
Sorting AlgorithmsBubble Sort, Selection Sort and Insertion Sort
Rasan Samarasinghe
Bubble Sort
Bubble Sort (C Implementation)
int abc[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
int c, i;
for (c = 1; c <= 9; c++){
for (i = 0; i <= 7; i++){
if (abc[i] > abc[i + 1]){
int temp = abc[i];
abc[i] = abc[i + 1];
abc[i + 1] = temp;
}
}
}
Selection Sort
Selection Sort (C Implementation)
int abc[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
int minindex, i, c;
for (c = 0; c <= 8; c++){
minindex = c;
for (i = c; i <= 8; i++){
if (abc[i] < abc[minindex]){
minindex = i;
}
}
int temp = abc[c];
abc[c] = abc[minindex];
abc[minindex] = temp;
}
Insertion Sort
Insertion Sort (C Implementation)
int abc[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
int c, i, index;
for (c = 1; c <= 8; c++){
index = abc[c];
for (i = c; abc[i - 1] > index; i--){
abc[i] = abc[i - 1];
}
abc[i] = index;
}
The End
w: http://rasan.net e: [email protected] t: http://twitter.com/rasansmn