Archive for category C

Quick Sort

Und weiter mit den Sortierübungen, jetzt der Quicksort in C

?Download quicksort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
 
void quick_sort(int *liste, int links, int rechts);
void output(int *liste, int start, int ende);
 
int main(int argc, char* argv[])
{
 
	int a[] = {15, 9, 12, 1, 6, 13, 3, 4, 7, 10, 8, 11, 2, 5, 14, 16};
 
	printf("+Start:n");
	quick_sort(a,0,15);
	printf("+Ende:n");
	output(a,0,15);
 
 
	return 0;
}
void output(int *liste, int start, int ende)
{
	int i=0;
 
	for(i=start; i<=ende; i++)
	{
		printf("[%2d] ",liste[i]);
	}
	printf("n");
}
 
 
void quick_sort(int *liste, int in_links, int in_rechts)
{
	int links=in_links;
	int rechts=in_rechts;
	int mitte=liste[(links+rechts)/2];
	int temp=0;
	printf("+QSort: Von %d bis %d - Teile bei [%2d]:n  ",links,rechts, mitte);
	output(liste,links,rechts);
	do
	{
		while (liste[links] < mitte) links++;
		while (liste[rechts] > mitte) rechts--;
		if (links <= rechts)
		{
			printf("  Tausche [%2d] <--> [%2d]n  ", liste[links], liste[rechts]);
			temp = liste[links];
			liste[links] = liste[rechts];
			liste[rechts] = temp;
			output(liste,in_links,in_rechts);
			links++;
			rechts--;
		}
	} while (links <= rechts);
	if (rechts > in_links) quick_sort(liste, in_links, rechts);
	if (links < in_rechts) quick_sort(liste, links, in_rechts);
}

Tags: , ,

Bubble Sort

Eine einfache Implementierung des Bubble Sort Algorithmus in C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#define MAX 8
 
void bubble_sort(int *liste);
void output(int *liste);
 
int main(int argc, char* argv[])
{
 
	int a[] = {15, 9, 12, 1, 6, 13, 3, 4};
 
	output(a);
	bubble_sort(a);
 
	return 0;
}
void output(int *liste)
{
	int i=0;
 
	for(i=0; i<MAX; i++)
	{
		printf("[%2d] ",liste[i]);
	}
	printf("");
}
 
 
void bubble_sort(int *liste)
{
	int i=0;
	int j=0;
	int x=0;
 
	for(i=1; i<MAX; i++)
	{
		for(j=MAX-1; j>=i; j--)
		{
			if (liste[j-1] > liste[j])
			{
				printf(" Tausche: [%2d] > [%2d] \n",liste[j-1],liste[j]); 
				x = liste[j-1];
				liste[j-1] = liste[j];
				liste[j] = x;
				printf(" "); 
				output(liste);
			}
			else
			{
				//printf(" Vergleich: [%2d] < [%2d] \n",liste[j-1],liste[j]);
			}
		}
 
	}
 
}

Tags: , ,

Selection Sort

Die Implementierung von Selection Sort in C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#define MAX 8
 
void selection_sort(int *liste);
void output(int *liste);
 
int main(int argc, char* argv[])
{
 
	int a[] = {15, 9, 12, 1, 6, 13, 3, 4};
 
	output(a);
	selection_sort(a);
 
	return 0;
}
void output(int *liste)
{
	int i=0;
 
	for(i=0; i<MAX; i++)
	{
		printf("[%d] ",liste[i]);
	}
	printf("n");
}
 
 
void selection_sort(int *liste)
{
	int i=0;
	int j=0;
	int x=0;
	int k=0;
 
	for(i=0; i<MAX; i++)
	{
		k = i;
		x = liste[i];
 
		for(j=i+1; j<MAX; j++)
		{
			if (liste[j] < x)
			{
				k = j;
				x = liste[j];
			}
		}
		printf("   >> Tausche  [%d] mit  [%d] n",liste[i],x);
		liste[k] = liste[i];
		liste[i] = x;
		output(liste);
	}
 
}

Tags: , ,

Insertion Sort

Eine sehr einfache Implementierung eines Insertion Sorts in C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#define MAX 8
 
void insertion_sort(int *liste);
void output(int *liste);
 
int main(int argc, char* argv[])
{
 
	int a[] = {15, 9, 12, 1, 6, 13, 3, 4};
 
	output(a);
	insertion_sort(a);
 
	return 0;
}
void output(int *liste)
{
	int i=0;
 
	for(i=0; i<MAX; i++)
	{
		printf("[%2d] ",liste[i]);
	}
	printf("n");
}
 
 
void insertion_sort(int *liste)
{
	int i=0;
	int j=0;
	int x=0;
 
	for(i=1; i<MAX; i++)
	{
		x = liste[i];
		j = i-1;
 
		while (j >= 0 && x < liste[j])
		{
			printf(" Vergleich: [%2d] < [%2d] n",x,liste[j]); 
			liste[j+1] = liste[j];
			//printf("   +> "); 
			//output(liste);
			j--;
		}
 
		liste[j+1] = x;
		printf(" Aufbau:    "); 
		output(liste);
	}
 
}

Tags: , ,

Shell Sort

Die Implementierung von Shell Sort

?Download shell_sort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#define MAX 8
 
void shell_sort(int *liste);
void output(int *liste);
 
int main(int argc, char* argv[]) {
	int a[] = {15, 9, 12, 1, 6, 13, 3, 4};
	output(a);
	shell_sort(a);
	return 0;
}
 
void output(int *liste) {
	int i=0;
	for(i=0; i<MAX; i++)
	{
		printf("[%2d] ",liste[i]);
	}
	printf("\n");
}
 
void shell_sort(int *liste) {
	int i=0;
	int j=0;
	int k=0;
	int h=0;
	int t=0;
	const int anz_schritte = 4;
	int schritte[] = {9, 6, 3, 1};
 
	for(k=0; k < anz_schritte; k++)	{
		h = schritte[k];
		printf("Schrittweite: %2d\n",h);
		for (i=h; i < MAX; i++)	{
			j = i;
			t = liste[j];
			while (j >= h && liste[j-h]>t) {
				printf(" Vergleich: [%2d] > [%2d] \n",liste[j-h],t); 
				liste[j] = liste[j-h];
				j = j-h;
			}
			liste[j]=t;
		}
		printf(" Aufbau:    "); 
		output(liste);
	}
 
}

Tags: , ,

All form fields are required.







All form fields are required.