8

Click here to load reader

Sorting Algorithms

Embed Size (px)

DESCRIPTION

Sorting Algorithms for Certificate in C/C++ Programming - ESOFT Metro Campus

Citation preview

Page 1: Sorting Algorithms

Sorting AlgorithmsBubble Sort, Selection Sort and Insertion Sort

Rasan Samarasinghe

Page 2: Sorting Algorithms

Bubble Sort

Page 3: Sorting Algorithms

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;

}

}

}

Page 4: Sorting Algorithms

Selection Sort

Page 5: Sorting Algorithms

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;

}

Page 6: Sorting Algorithms

Insertion Sort

Page 7: Sorting Algorithms

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;

}

Page 8: Sorting Algorithms

The End

w: http://rasan.net e: [email protected] t: http://twitter.com/rasansmn