c12-1.c
/* code: c12-1.c (v1.18.00) */
#include <stdio.h>
#include <stdlib.h>
/* ------------------------------------------ */
void print_array (int v[], int n) {
int i;
printf ("array: ");
for (i = 0; i < n; i++) {
printf ("%d ", v[i]);
}
printf ("\n");
}
/* ------------------------------------------ */
void bubble_sort (int v[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (v[j] > v[j + 1]) {
temp = v[j];
v[j] = v[j + 1];
v[j + 1] = temp;
}
printf ("i:%d j:%d ", i, j);
print_array (v, n);
}
}
}
/* ------------------------------------------ */
int main () {
int array[5]
= { 300, 100, 200, 500, 400 };
print_array (array, 5);
bubble_sort (array, 5);
print_array (array, 5);
return 0;
}
c12-2.c
/* code: c12-2.c (v1.18.00) */
#include <stdio.h>
#include <stdlib.h>
/* ------------------------------------------ */
void print_array (int v[], int n) {
int i;
printf ("array: ");
for (i = 0; i < n; i++) {
printf ("%d ", v[i]);
}
printf ("\n");
}
/* ------------------------------------------ */
void selection_sort (int v[], int n) {
int i, j, t, min_index;
for (i = 0; i < n - 1; i++) {
min_index = i;
for (j = i + 1; j < n; j++) {
if (v[j] < v[min_index]) {
min_index = j;
}
printf ("i:%d j:%d ", i, j);
print_array (v, n);
}
t = v[i];
v[i] = v[min_index];
v[min_index] = t;
}
}
/* ------------------------------------------ */
int main () {
int array[5]
= { 300, 100, 200, 500, 400 };
print_array (array, 5);
selection_sort (array, 5);
print_array (array, 5);
return 0;
}
c12-3.c
/* code: c12-3.c (v1.18.00) */
#include <stdio.h>
#include <stdlib.h>
/* ------------------------------------------ */
void print_array (int v[], int n) {
int i;
printf ("array: ");
for (i = 0; i < n; i++) {
printf ("%d ", v[i]);
}
printf ("\n");
}
/* ------------------------------------------ */
void insertion_sort (int v[], int n) {
int i, j, t;
for (i = 1; i < n; i++) {
j = i;
while ((j >= 1) && (v[j - 1] > v[j])) {
t = v[j];
v[j] = v[j - 1];
v[j - 1] = t;
j--;
printf ("i:%d j:%d ", i, j);
print_array (v, n);
}
}
}
/* ------------------------------------------ */
int main () {
int array[5]
= { 300, 100, 200, 500, 400 };
print_array (array, 5);
insertion_sort (array, 5);
print_array (array, 5);
return 0;
}
c12-4.c
/* code: c12-4.c (v1.18.00) */
#include <stdio.h>
#include <stdlib.h>
/* ------------------------------------------ */
void print_array (int v[], int n) {
int i;
printf ("array: ");
for (i = 0; i < n; i++) {
printf ("%d ", v[i]);
}
printf ("\n");
}
/* ------------------------------------------ */
void insertion_sort (int v[], int n) {
int i, j, t;
for (i = 1; i < n; i++) {
j = i;
while ((j >= 1) && (v[j - 1] > v[j])) {
t = v[j];
v[j] = v[j - 1];
v[j - 1] = t;
j--;
printf ("i:%d j:%d ", i, j);
print_array (v, n);
}
}
}
/* ------------------------------------------ */
int main () {
int array[5]
= { 100, 200, 300, 400, 500 };
print_array (array, 5);
insertion_sort (array, 5);
print_array (array, 5);
return 0;
}
c12-5.c
/* code: c12-5.c (v1.18.00) */
#include <stdio.h>
#include <stdlib.h>
/* ------------------------------------------ */
void print_array (int v[], int n) {
int i;
printf ("array: ");
for (i = 0; i < n; i++) {
printf ("%d ", v[i]);
}
printf ("\n");
}
/* ------------------------------------------ */
void insertion_sort (int v[], int n) {
int i, j, t;
for (i = 1; i < n; i++) {
j = i;
while ((j >= 1) && (v[j - 1] > v[j])) {
t = v[j];
v[j] = v[j - 1];
v[j - 1] = t;
j--;
printf ("i:%d j:%d ", i, j);
print_array (v, n);
}
}
}
/* ------------------------------------------ */
int main () {
int array[5]
= { 500, 400, 300, 200, 100 };
print_array (array, 5);
insertion_sort (array, 5);
print_array (array, 5);
return 0;
}
q12-1.c
/* code: q12-1.c (v1.18.00) */
#include <stdio.h>
#include <stdlib.h>
/* ------------------------------------------ */
void print_array (int v[], int n) {
int i;
printf ("array: ");
for (i = 0; i < n; i++) {
printf ("%d ", v[i]);
}
printf ("\n");
}
/* ------------------------------------------ */
void swap (int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
/* ------------------------------------------ */
void bubble_sort (int v[], int n) {
int i, j;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (v[j] > v[j + 1]) {
swap (&v[j], &v[j + 1]);
}
printf ("i:%d j:%d ", i, j);
print_array (v, n);
}
}
}
/* ------------------------------------------ */
int main () {
int array[5]
= { 300, 100, 200, 500, 400 };
print_array (array, 5);
bubble_sort (array, 5);
print_array (array, 5);
return 0;
}
q12-2.c
/* code: q12-2.c (v1.18.00) */
#include <stdio.h>
#include <stdlib.h>
/* ------------------------------------------ */
void print_array (int v[], int n) {
int i;
printf ("array: ");
for (i = 0; i < n; i++) {
printf ("%d ", v[i]);
}
printf ("\n");
}
/* ------------------------------------------ */
void bubble_sort (int v[], int n) {
int i, j, temp, flag;
flag = 1;
for (i = 0; (i < n - 1) && (flag == 1); i++) {
flag = 0;
for (j = 0; j < n - i - 1; j++) {
if (v[j] > v[j + 1]) {
temp = v[j];
v[j] = v[j + 1];
v[j + 1] = temp;
flag = 1;
}
printf ("i:%d j:%d ", i, j);
print_array (v, n);
}
}
}
/* ------------------------------------------ */
int main () {
int array[5]
= { 300, 100, 200, 500, 400 };
print_array (array, 5);
bubble_sort (array, 5);
print_array (array, 5);
return 0;
}
q12-3.c
/* code: q12-3.c (v1.18.00) */
#include <stdio.h>
#include <stdlib.h>
/* ------------------------------------------ */
void print_array (int v[], int n) {
int i;
printf ("array: ");
for (i = 0; i < n; i++) {
printf ("%d ", v[i]);
}
printf ("\n");
}
/* ------------------------------------------ */
void bubble_sort (int v[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (v[j] > v[j + 1]) {
temp = v[j];
v[j] = v[j + 1];
v[j + 1] = temp;
}
}
}
}
/* ------------------------------------------ */
int main () {
int *array;
int i, n;
n = 100000;
array = malloc (sizeof (int) * n);
for (i = 0; i < n; i++) {
array[i] = rand () % n;
}
print_array (array, 10);
bubble_sort (array, n);
print_array (array, 10);
free (array);
return 0;
}
EOF