Olá bom dia amigo, segue um código que fiz a algum tempo espero que ajude a dar uma clareada no que precisa. Lembrando que fiz como uma TAD.
----------------main.c----------------
#include <stdio.h>
#include <stdlib.h>
#include "Ordenacao.h"
#define VET_TAM_MAX 100000
#define VET_TAM_MIN 10
int main()
{
int vet_tam;
do {
printf("Informe tamanho do vetor [%d - %d]\n", VET_TAM_MIN, VET_TAM_MAX);
scanf(" %d", &vet_tam);
} while (vet_tam > VET_TAM_MAX || vet_tam < VET_TAM_MIN);
printf("Criando vetor de %d posições...\n\n", vet_tam);
int *vetor;
vetor = cria_vetor(vet_tam);
printf("**** VETOR ORIGINAL: ****\n");
mostra_vetor(vetor, 0, vet_tam - 1);
bubble_sort(vetor, vet_tam);
printf("**** VETOR ORDENADO (Bubble): ****\n");
mostra_vetor(vetor, 0, vet_tam - 1);
return 0;
}
----------------Ordenacao.c----------------
#include <stdlib.h>
#include <stdio.h>
#include "Ordenacao.h"
/*
****** Metodos auxiliares ******
*/
int* cria_vetor(int tam) {
int *vet = (int *)calloc(tam, sizeof(int));
int i;
srand(time(0));
for(i = 0; i < tam; i++){
vet[i] = rand()%(tam+200);
}
return vet;
}
void mostra_vetor(int vetor[], int ini, int fim) {
int i;
int tam = fim - ini + 1;
int step = (tam / 100) + 1;
char dots[5] = "";
if (step > 1) {
strcpy(dots, "... ");
}
printf("\n*** Vetor (inicio) ***\n");
for(i = ini; i <= fim; i += step){
printf("%d %s", vetor[i], dots);
}
printf("\n*** Vetor (fim) ***\n\n");
}
/* Bubble Sort */
void bubble_sort(int vetor[], int tamanho) {
int n, swapped, i, aux;
n = tamanho;
do {
swapped = 0;
for (i = 1; i <= n-1; i++) {
if (vetor[i-1] > vetor[i]) {
aux = vetor[i-1];
vetor[i-1] = vetor[i];
vetor[i] = aux;
swapped = i;
}
}
n = swapped;
} while (swapped != 0);
}
----------------Ordenacao.h----------------
/*
* Arquivo de definicao para biblioteca de métodos de ordenação
*/
#ifndef ORDENACAO_H
#define ORDENACAO_H
#include <time.h>
/*
* Metodos de ordenacao
*/
/* Bubble Sort */
void bubble_sort(int vetor[], int tamanho);
/* Selection Sort */
void selection_sort(int vetor[], int tamanho);
/* Funções Auxiliares */
int* cria_vetor(int tam);
void mostra_vetor(int vetor[], int ini, int fim);
#endif