Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define N 20000
- // Iterativ
- void insertionSort(int arr[], int n)
- {
- int i, j, key;
- for (i = 1; i < n; i++)
- {
- key = arr[i];
- j = i-1;
- while (j >= 0 && arr[j] > key)
- {
- arr[j+1] = arr[j];
- j = j-1;
- }
- arr[j+1] = key;
- }
- }
- // Rekursiv
- void insertionSortRecursive(int arr[], int n)
- {
- if (n <= 1)
- return;
- insertionSortRecursive(arr, n-1);
- int last = arr[n-1];
- int j = n-2;
- while (j >= 0 && arr[j] > last)
- {
- arr[j+1] = arr[j];
- j--;
- }
- arr[j+1] = last;
- }
- int main(){
- int A[N], B[N];
- int i;
- for(i = 0; i < N; i++){
- B[i] = A[i] = 1 + rand()%1000;
- }
- clock_t start, ende;
- double a_zeit;
- printf("InsertionSort Iterativ:\n");
- start = clock();
- insertionSort(A, N);
- ende = clock();
- a_zeit = (double)(ende-start)/CLOCKS_PER_SEC;
- printf("Zeit: %fs\n", a_zeit);
- printf("InsertionSort Rekursiv:\n");
- start = clock();
- insertionSortRecursive(B, N);
- ende = clock();
- a_zeit = (double)(ende-start)/CLOCKS_PER_SEC;
- printf("Zeit: %fs\n", a_zeit);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement