ex10-1.c
/* code: ex10-1.c (v1.16.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, t;
for (i = 0; i < n - 1; i++) {
for (j = n - 1; j > i; j--) {
if (v[j - 1] > v[j]) {
t = v[j];
v[j] = v[j - 1];
v[j - 1] = t;
}
printf ("i:%d j:%d ", i, j);
print_array (v, n);
}
}
}
/* ------------------------------------------ */
int main ()
{
int array[5]
= { 30, 50, 20, 10, 40 };
print_array (array, 5);
bubble_sort (array, 5);
print_array (array, 5);
return 0;
}
ex10-2.c
/* code: ex10-2.c (v1.16.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]
= { 30, 50, 20, 10, 40 };
print_array (array, 5);
selection_sort (array, 5);
print_array (array, 5);
return 0;
}
ex10-3.c
/* code: ex10-3.c (v1.16.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]
= { 30, 50, 20, 10, 40 };
print_array (array, 5);
insertion_sort (array, 5);
print_array (array, 5);
return 0;
}
ex10-4.c
/* code: ex10-4.c (v1.16.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]
= { 10, 20, 30, 40, 50 };
print_array (array, 5);
insertion_sort (array, 5);
print_array (array, 5);
return 0;
}
ex10-5.c
/* code: ex10-5.c (v1.16.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] = { 50, 40, 30, 20, 10 };
print_array (array, 5);
insertion_sort (array, 5);
print_array (array, 5);
return 0;
}
q10-1.c
/* code: q10-1.c (v1.16.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, t, flag;
flag = 1;
for (i = 0; (i < n - 1) && (flag == 1); i++) {
flag = 0;
for (j = n - 1; j > i; j--) {
if (v[j - 1] > v[j]) {
t = v[j];
v[j] = v[j - 1];
v[j - 1] = t;
flag = 1;
}
printf ("i:%d j:%d ", i, j);
print_array (v, n);
}
}
}
/* ------------------------------------------ */
int main ()
{
/* int array[5]
= { 30, 50, 20, 10, 40 }; */
/* int array[5]
= { 50, 40, 30, 20, 10 }; */
int array[5]
= { 10, 20, 30, 40, 50 };
print_array (array, 5);
bubble_sort (array, 5);
print_array (array, 5);
return 0;
}
q10-2.c
/* code: q10-2.c (v1.16.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");
}
/* ------------------------------------------ */
int int_compare (const void *va, const void *vb)
{
int a, b;
a = *(int *) va;
b = *(int *) vb;
if (a < b) {
return (-1);
}
else if (a > b) {
return (1);
}
else {
return (0);
}
}
/* ------------------------------------------ */
int main ()
{
int array[5]
= { 30, 50, 20, 10, 40 };
print_array (array, 5);
qsort (array, 5, sizeof (int), int_compare);
print_array (array, 5);
return 0;
}
q10-3.c
/* code: q10-3.c (v1.16.00) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* ------------------------------------------ */
int cmp_string (const void *p1, const void *p2)
{
return strcmp (*(char *const *) p1, *(char *const *) p2);
}
/* ------------------------------------------ */
void print_str_array (char *v[], int n)
{
int i;
printf ("array: ");
for (i = 0; i < n; i++) {
printf ("%s ", v[i]);
}
printf ("\n");
}
/* ------------------------------------------ */
int main ()
{
char *array[7] = {
"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday",
};
print_str_array (array, 7);
qsort (array, 7, sizeof (char *), cmp_string);
print_str_array (array, 7);
return 0;
}
EOF