0% found this document useful (0 votes)
12 views8 pages

CTDL05 Sortalgorithm

Uploaded by

hathanhvu16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

CTDL05 Sortalgorithm

Uploaded by

hathanhvu16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

#include <stdio.

h>
#include <stdlib.h>

void swap(int *a, int *b){


int tmp = *a;
*a = *b;
*b = tmp;
}

void copy(int *a, int *b, int n){


for (int i = 0; i < n; i++)
b[i] = a[i];
}

void output(int *a, int n){


for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}

void bubbleSort(int *a, int n, int *steps){


for (int i = 0; i < n - 1; i++){
for (int j = i + 1; j < n; j++){
(*steps)++; // Buoc so sanh
if (a[i] > a[j]){
(*steps)++; // Buoc hoan vi
swap(a + i, a + j);
}
}
}
}
void selectionSort(int *a, int n, int *steps){
int pos;
for (int i = 0; i < n - 1; i++){
pos = i;
for (int j = i + 1; j < n; j++){
(*steps)++; // Dem so buoc so sanh
if (a[pos] > a[j]){
pos = j;
}
}
if (pos != i){
(*steps)++; // Dem so buoc hoan vi
swap(a + pos, a + i);
}
}
}

void insertionSort(int *a, int n, int *steps){


int j, key;
for (int i = 1; i < n; i++){
j = i - 1, key = a[i];
while (j >= 0){
(*steps)++; // Dem so buoc so sanh
if (a[j] > key){
a[j + 1] = a[j];
(*steps)++; // Dem so buoc gan
j--;
}
else break;
}
a[j + 1] = key;
}
}

int main(){
int n = 10;
int a[10] = {3, 1, 8, 2, 6, 5, 3, 9, 1, 0};
int b[10], c[10], stepsBub = 0, stepsSel = 0, stepsIns = 0;
copy(a, b, n); copy(a, c, n);

//Bubble Sort
bubbleSort(a, n, &stepsBub);
printf("Steps of bubble sort: %d\n", stepsBub);

//Selection Sort
selectionSort(b, n, &stepsSel);
printf("Steps of selection sort: %d\n", stepsSel);

//Insertion Sort
insertionSort(c, n, &stepsIns);
printf("Steps of insertion sort: %d\n", stepsIns);
return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void copy(int *a, int *b, int n){


for (int i = 0; i < n; i++)
b[i] = a[i];
}

void swap(int *a, int *b){


int tmp = *a;
*a = *b;
*b = tmp;
}

void generate(int *a, int n){


for (int i = 0; i < n; i++){
a[i] = rand() % 100;
}
}

void output(int *arr, int n){


for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

void bubbleSort(int *a, int n){


for (int i = 0; i < n - 1; i++){
for (int j = i + 1; j < n; j++){
if (a[i] < a[j])
swap(a + i, a + j);
}
}
}

void selectionSort(int *a, int n){


int pos;
for (int i = 0; i < n - 1; i++){
pos = i;
for (int j = i + 1; j < n; j++){
if (a[j] > a[pos])
pos = j;
}
swap(a + i, a + pos);
}
}

void insertionSort(int *a, int n){


int j, key;
for (int i = 0; i < n; i++){
j = i - 1, key = a[i];
while (j >= 0 && a[j] < key){
a[j + 1] = a[j];
j--;
}
a[j + 1] = key;
}
}

int main(){
srand(time(NULL));
int n = 20;
int a[25], b[25], c[25];
generate(a, n);
printf("Mang ban dau: ");
output(a, n);
copy(a, b, n); copy(a, c, n);

printf("Mang sau khi sap xep:\n\n");


bubbleSort(a, n);
output(a, n);

selectionSort(b, n);
output(b, n);
insertionSort(c, n);
output(c, n);
return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void swap(int *a, int *b){


int tmp = *a;
*a = *b;
*b = tmp;
}

void generate(int *a, int n){


for (int i = 0; i < n; i++){
a[i] = rand() % 100;
}
}

void copy(int *a, int *b, int n){


for (int i = 0; i < n; i++)
b[i] = a[i];
}

void output(int *a, int n){


for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}

void bubbleSort(int *a, int n, int *steps){


for (int i = 0; i < n - 1; i++){
for (int j = i + 1; j < n; j++){
(*steps)++; // Buoc so sanh
if (a[i] > a[j]){
(*steps)++; // Buoc hoan vi
swap(a + i, a + j);
}
}
}
}

void selectionSort(int *a, int n, int *steps){


int pos;
for (int i = 0; i < n - 1; i++){
pos = i;
for (int j = i + 1; j < n; j++){
(*steps)++; // Dem so buoc so sanh
if (a[pos] > a[j]){
pos = j;
}
}
if (pos != i){
(*steps)++; // Dem so buoc hoan vi
swap(a + pos, a + i);
}
}
}

void insertionSort(int *a, int n, int *steps){


int j, key;
for (int i = 1; i < n; i++){
j = i - 1, key = a[i];
while (j >= 0){
(*steps)++; // Dem so buoc so sanh
if (a[j] > key){
a[j + 1] = a[j];
(*steps)++; // Dem so buoc gan
j--;
}
else break;
}
a[j + 1] = key;
}
}

int main(){
srand(time(NULL));
int n[4] = {10, 20, 50, 100};
int a[105], b[105], c[105], sumStepsBub, sumStepsSel, sumStepsIns;
int stepsBub, stepsSel, stepsIns;

for (int i = 0; i < 4; i++){


printf("n = %d\n", n[i]);
sumStepsBub = 0, sumStepsSel = 0, sumStepsIns = 0;
for (int j = 0; j < 10000; j++){
generate(a, n[i]);
copy(a, b, n[i]); copy(a, c, n[i]);
stepsBub = 0, stepsSel = 0, stepsIns = 0;
bubbleSort(a, n[i], &stepsBub);
selectionSort(b, n[i], &stepsSel);
insertionSort(c, n[i], &stepsIns);
sumStepsBub += stepsBub;
sumStepsIns += stepsIns;
sumStepsSel += stepsSel;
}
printf("Steps bubble sort average: %.4f\n", 1.0 * sumStepsBub / 10000);
printf("Steps insertion sort average: %.4f\n", 1.0 * sumStepsIns /
10000);
printf("Steps selection sort average: %.4f\n", 1.0 * sumStepsSel /
10000);
}
return 0;
}

You might also like