Users Online

· Guests Online: 30

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C Examples on Pancake Sort, Bogo Sort and Shell Sort

C Examples on Pancake Sort, Bogo Sort and Shell Sort

 

The C programs in this section illustrate Pancake Sort, BogoSort and Shell Sort algorithms. In Pancake Sort, instead of individual elements being sorted, the only operation allowed is to “flip” one end of the list. Bogo Sort shuffles a list of values as long as they are not sorted. Shell sort is a generalization of insertion sort that allows the exchange of items that are far apart.

 

 

C Program to Implement Pancake Sort on Array of Integers 
C Program to Implement BogoSort in an Integer Array 
C Program to Perform Shell Sort without using Recursion

 

 

 

C Program to Implement Pancake Sort on Array of Integers

This C Program Implements Pancake Sort on Array of Integers.Pancake sorting is a variation of the sorting problem in which the only allowed operation is to reverse the elements of some prefix of the sequence.

 

Here is source code of the C Program to Implement Pancake Sort on Array of Integers. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Implement Pancake Sort on Array of Integers
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. void do_flip(int *, int, int);
  8.  
  9. /*Function to implement the pancake sort*/
  10. int pancake_sort(int *list, unsigned int length)
  11. {
  12.     if (length < 2)
  13.         return 0;
  14.     int i, a, max_num_pos, moves;
  15.  
  16.     moves = 0;
  17.     for (i = length;i > 1;i--)
  18.     {
  19.         max_num_pos = 0;
  20.         for (a = 0;a < i;a++)
  21.         {
  22.             if (list[a] > list[max_num_pos])
  23.                 max_num_pos = a;
  24.         }
  25.         if (max_num_pos ==  i - 1)
  26.             continue;
  27.         if (max_num_pos)
  28.         {
  29.             moves++;
  30.             do_flip(list, length, max_num_pos + 1);
  31.         }
  32.         do_flip(list, length, i);
  33.     }
  34.     return moves;
  35. }
  36.  
  37. /*Function to do flips in the elements*/
  38. void do_flip(int *list,  int length,  int num)
  39. {
  40.     int swap;
  41.     int i = 0;
  42.     for (i;i < --num;i++)
  43.     {
  44.         swap = list[i];
  45.         list[i] = list[num];
  46.         list[num] = swap;
  47.     }
  48. }
  49.  
  50. /*Function to print the array*/
  51. void print_array(int list[], int length)
  52. {
  53.     int i;
  54.     for (i = 0;i < length;i++)
  55.     {
  56.         printf("%d ", list[i]);
  57.     }
  58. }
  59.  
  60. int main(int argc,  char **argv)
  61. {
  62.    int list[9];
  63.    int i;
  64.    printf("enter the 9 elements of array:\n");
  65.    for (i = 0;i < 9;i++)
  66.        scanf("%d", &list[i]);
  67.    printf("\nOriginal: ");
  68.    print_array(list, 9);
  69.    int moves  =  pancake_sort(list, 9);
  70.    printf("\nSorted: ");
  71.    print_array(list, 9);
  72.    printf(" - with a total of %d moves\n",  moves);
  73. }

 

$ cc pancake.c
$ a.out
enter the 9 elements of array:
10
9
8
7
6
5
4
3
2
 
Original: 10 9 8 7 6 5 4 3 2
Sorted: 2 3 4 5 6 7 8 9 10   - with a total of 0 moves
$ a.out
enter the 9 elements of array:
1
2
3
4
5
6
7
8
9
 
Original: 1 2 3 4 5 6 7 8 9
Sorted: 1 2 3 4 5 6 7 8 9   - with a total of 0 moves
$ a.out
enter the 9 elements of array:
5
6
7
8
9
1
4
2
3
Original: 5 6 7 8 9 1 4 2 3
Sorted: 1 2 3 4 5 6 7 8 9   - with a total of 3 moves

 

 
 
 

C Program to Implement BogoSort in an Integer Array

This C Program Implements BogoSort in an Integer Array.

 

Here is source code of the C Program to Implement BogoSort in an Integer Array. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Implement BogoSort in an Integer Array
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. #define size 7
  8. /* Function Prototypes */
  9.  
  10. int is_sorted(int *, int);
  11. void shuffle(int *, int); 
  12. void bogosort(int *, int);
  13.  
  14. int main()
  15. {
  16.     int numbers[size];
  17.     int i;
  18.  
  19.     printf("Enter the elements of array:");
  20.     for (i = 0; i < size;i++)
  21.     {
  22.         scanf("%d", &numbers[i]);
  23.     }
  24.     bogosort(numbers, size);
  25.     printf("The array after sorting is:");
  26.     for (i = 0;i < size;i++)
  27.     {
  28.         printf("%d\n", numbers[i]);
  29.     }
  30.     printf("\n");
  31. }
  32.  
  33. /* Code to check if the array is sorted or not */
  34. int is_sorted(int *a, int n)
  35. {
  36.     while (--n >= 1)
  37.     {
  38.         if (a[n] < a[n - 1])
  39.         {
  40.             return 0;
  41.           }
  42.     }
  43.       return 1;
  44. }
  45.  
  46. /* Code to shuffle the array elements */
  47. void shuffle(int *a, int n)
  48. {
  49.     int i, t, temp;
  50.     for (i = 0;i < n;i++)
  51.     {
  52.         t = a[i];
  53.         temp = rand() % n;    /* Shuffles the given array using Random function */
  54.         a[i] = a[temp];
  55.         a[temp] = t;
  56.     }
  57. }
  58.  
  59. /* Code to check if the array is sorted or not and if not sorted calls the shuffle function to shuffle the array elements */
  60. void bogosort(int *a, int n)
  61. {
  62.     while (!is_sorted(a, n))
  63.     {
  64.         shuffle(a, n);
  65.     }
  66. }

 

$ cc bogo_sort.c
Average case:
$ a.out
Enter the elements of array:56
34
96
26
08
87
36
The array after sorting is:8
26
34
36
56
87
96
 
Best case:
$ a.out
Enter the elements of array:12
23
34
45
56
67
78
The array after sorting is:12
23
34
45
56
67
78
 
Worst case:
$ a.out
Enter the elements of array:984
38
983
389
37
596
483
The array after sorting is:37
38
389
483
596
983
984

C Program to Perform Shell Sort without using Recursion

This C Program Performs Shell Sort without using Recursion.

 

Here is source code of the C Program to Perform Shell Sort without using Recursion. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Perform Shell Sort without using Recursion
  3.  */
  4. #include  <stdio.h>
  5. #define size 7
  6.  
  7. /* Function Prototype */
  8. int shell_sort(int []);
  9.  
  10. void main()
  11. {
  12.     int arr[size], i;
  13.     printf("Enter the elements to be sorted:");
  14.     for (i = 0;i < size;i++)
  15.     {
  16.         scanf("%d", &arr[i]);
  17.     }
  18.     shell_sort(arr);
  19.     printf("The array after sorting is:");
  20.     for (i = 0;i < size;i++)
  21.     {
  22.         printf("\n%d", arr[i]);
  23.     }
  24. }
  25.  
  26. /* Code to sort array using shell sort */
  27. int shell_sort(int array[])
  28. {
  29.     int i = 0, j = 0, k = 0, mid = 0;
  30.     for (k = size / 2;k > 0;k /= 2)
  31.     {
  32.         for (j = k;j < size;j++)
  33.         {
  34.             for (i = j - k;i >= 0;i -= k)
  35.             {
  36.                 if (array[i + k] >= array[i])
  37.                 {
  38.                     break;
  39.                 }
  40.                 else
  41.                 {
  42.                     mid = array[i];
  43.                     array[i] = array[i + k];
  44.                     array[i + k] = mid;
  45.                 }
  46.             }
  47.         }
  48.     }
  49.     return 0;
  50. }

 

$ cc shellsort.c
Average case:
$ a.out
Enter the elements to be sorted:57
67
48
93
42
84
95
The array after sorting is:
42
48
57
67
84
93
95
 
Best case:
$ a.out
Enter the elements of array:22
33
74
85
86
87
98
The array after sorting is:22
33
74
85
86
87
98
 
Worst case:
$ a.out
Enter the elements of array:94
92
91
89
85
80
43
The array after sorting is:43
80
85
89
91
92
94


 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.81 seconds
10,798,909 unique visits