Counting Sort

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define MAX_VALUE 10001

int n, m;
int a[MAX_VALUE];

int main()
{
    printf("Count : ");
    scanf("%d", &n);
    printf("\r\n\r\n");
    for (int i = 0; i < n; i++) {
        printf("a[%d] : ", i);
        scanf("%d", &m); 
        a[m]++; 
    }
    printf("\r\n\r\n");
    printf("Result :");
    printf("\r\n");
    for (int i = 0; i < MAX_VALUE; i++) {
        while (a[i] != 0) {
            printf("%d ", i);
            a[i]--;
        }
    }

    printf("\r\n\r\n");
    system("pause");
}

 

 

be the happy Gosu.

woojja ))*

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

 

반응형

'ETC > C, C++' 카테고리의 다른 글

[C, C++] Counting Sort  (2) 2022.02.09
[C, C++] Selection Sort, Insertion Sort  (0) 2021.11.04
[C, C++] Queue in Array, Linked List  (0) 2021.10.20
[C, C++] Stack in Array, Lined List  (0) 2021.10.20
[C, C++] Sorted Doubly Linked List  (0) 2021.10.20
  1. Favicon of http://www.sysnet.pe.kr BlogIcon kevin 2022.02.15 15:43

    요즘 C++ 하시나봐요? ^^

1. Selection Sort

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <limits.h>

#define SIZE 1000

int a[SIZE];

int swap(int *a, int *b) {
	int temp = *a;
	*a = *b;
	*b = temp;
}

int main(void) {
	int n, min, index;
	scanf("%d", &n);

	for (int i = 0; i < n; i++) scanf("%d", &a[i]);

	for (int i = 0; i < n; i++) {
		min = INT_MAX;
		for (int j = i; j < n; j++) {
			if (min > a[j]) {
				min = a[j];
				index = j;
			}
		}
		swap(&a[i], &a[index]);
	}
	for (int i = 0; i < n; i++) {
		printf("%d", a[i]);
	}
	system("pause");
	return 0;
}

 

2. Insertion Sort

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <limits.h>

#define SIZE 1000

int a[SIZE];

int swap(int *a, int *b) {
	int temp = *a;
	*a = *b;
	*b = temp;
}

int main(void) {
	int n;
	scanf("%d", &n);

	for (int i = 0; i < n; i++) scanf("%d", &a[i]);

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


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

 

be the happy Gosu.

woojja ))*

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

반응형

'ETC > C, C++' 카테고리의 다른 글

[C, C++] Counting Sort  (2) 2022.02.09
[C, C++] Selection Sort, Insertion Sort  (0) 2021.11.04
[C, C++] Queue in Array, Linked List  (0) 2021.10.20
[C, C++] Stack in Array, Lined List  (0) 2021.10.20
[C, C++] Sorted Doubly Linked List  (0) 2021.10.20

+ Recent posts