Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
odd_even_sort.c File Reference

Odd Even Sort implementation More...

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <inttypes.h>
Include dependency graph for odd_even_sort.c:

Functions

void swap (int32_t *first, int32_t *second)
 for assert
 
void oddEvenSort (int *arr, int size)
 oddEvenSort sorts the array using the algorithm described above.
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Odd Even Sort implementation

Author
Edwin Ajong

This algorithm is divided into two phases- Odd and Even Phase. The algorithm runs until the array elements are sorted and in each iteration two phases occurs- Odd and Even Phases. In the odd phase, we perform a bubble sort on odd indexed elements and in the even phase, we perform a bubble sort on even indexed elements. Time Complexity: O(N ^ 2)

Function Documentation

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
117{
118 test(); // run self-test implementations
119 return 0;
120}
static void test()
Self-test implementations.
Definition odd_even_sort.c:90
Here is the call graph for this function:

◆ oddEvenSort()

void oddEvenSort ( int *  arr,
int  size 
)

oddEvenSort sorts the array using the algorithm described above.

A boolean varaible(isSorted) is declared and initialised to "false". In the while loop, the variable(isSorted) is then set to "true". During even phase the for loop loops through the array, touching just the even indexes. i.e arr[0], arr[2], arr[4] and so on. While during the odd phase, the for loop loops through the array, touching just the odd indexes. i.e arr[1], arr[3], arr[5] and so on. During these phases, if the if statement check if the interger at the current position in the array is greater than the interger at the next array index (i.e arr[index + 2], to make sure the index is odd during the odd phase and even during the even phase). If the condition is true, the function "swap" is called and address of the intergers in question are passed as parameters. After the swap is completed, "isSorted" is set to "false". The while loop will keep running till the array is propertly sorted.

Parameters
arrarray to be sorted
sizethe size of the array
Returns
void
53{
54 bool isSorted = false;
55 while(!isSorted)
56 {
57 isSorted = true;
58 int32_t i;
59
60 // Even phase
61 for(i = 0; i <= size - 2; i += 2)
62 {
63 if(arr[i] > arr[i + 1])
64 {
65 swap(&arr[i], &arr[i + 1]);
66 isSorted = false;
67 }
68 }
69
70 // Odd phase
71 for(i = 1; i <= size - 2; i += 2)
72 {
73 if(arr[i] > arr[i + 1])
74 {
75 swap(&arr[i], &arr[i + 1]);
76 isSorted = false;
77 }
78 }
79 }
80}

◆ swap()

void swap ( int32_t *  first,
int32_t *  second 
)

for assert

for bool for IO operations for dynammic memory allocation for random number generation for int32_t types

Swap numbers by reference(using pointers)

Parameters
firstpointer to first number
secondpointer to second number
Returns
void
27{
28 int32_t temp = *first;
29 *first = *second;
30 *second = temp;
31}

◆ test()

static void test ( void  )
static

Self-test implementations.

Two tests (unsorted) arrays were created and their corresponding solution(sorted) arrays were also created. The test arrays and their respective sizes are then passed in to the oddEvenSort function. To test if the algorithm works, a for loop is assigned to loop through the both arrays(test and solution) and check if the array elements of the test array correspond to the elements of the solution array.

Returns
void
91{
92 int32_t arr1[] = {-9, 2, 3, 1};
93 int32_t arr1Soln[] = {-9, 1, 2, 3};
94 int32_t arr2[] = {9, 7, 5, 3, 8, 2, 1, 4, 0, 6};
95 int32_t arr2Soln[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
96
97 oddEvenSort(arr1, 4);
98 oddEvenSort(arr2, 10);
99
100 for (int32_t i = 0; i < 4; i++)
101 {
102 assert(arr1[i] == arr1Soln[i]);
103 }
104
105 for (int32_t i = 0; i < 10; i++)
106 {
107 assert(arr2[i] == arr2Soln[i]);
108 }
109 printf("All tests have passed!\n");
110}
void oddEvenSort(int *arr, int size)
oddEvenSort sorts the array using the algorithm described above.
Definition odd_even_sort.c:52
Here is the call graph for this function: