Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * C Program that uses pointers to sort an array
- * in ascending order using Shell Sort
- */
- #include <exec/types.h>
- #include <dos/dos.h>
- #include <proto/exec.h>
- #include <proto/dos.h>
- #include <stdio.h>
- /* proto types */
- void swap(int *a, int *b);
- void shellsort(int arr[], int nums);
- void swap(int *a, int *b)
- {
- *a=*a + *b;
- *b=*a - *b;
- *a=*a - *b;
- return ;
- }
- // Function definition of sort array using shell sort
- void shellsort(int arr[], int nums)
- {
- int i = 0, j = 0, k = 0;
- // i -> gap/interval
- for (i = nums / 2; i > 0; i = i / 2)
- {
- // Traverse j till we reach the end of the sublist.
- for (j = i; j < nums; j++)
- {
- for(k = j - i; k >= 0; k = k - i)
- {
- if (arr[k+i] >= arr[k])
- {
- break;
- }
- else
- {
- swap(&arr[k], &arr[k+i]);
- }
- }
- }
- }
- return ;
- }
- int main()
- {
- int nums = 0;
- int k = 0;
- #ifdef __SASC
- int arr[256];
- #else
- int arr[nums];
- #endif
- printf("Enter total no. of elements : ");
- #ifdef __SASC
- printf("(max. allowed array size is 256)");
- #endif
- scanf("%d", &nums);
- #ifdef __SASC
- if (nums > 256)
- {
- puts("Sorry, that's too much for me!");
- puts("Yours sincerely,\nSAS/C");
- return 0;
- }
- #endif
- printf("Enter the array: \n");
- //scan the array elements.
- for (k = 0 ; k < nums; k++)
- {
- scanf("%d", &arr[k]);
- }
- //Call the function of shell sort, bypassing the array base pointer to the first element.
- shellsort(arr, nums);
- // After the sorting the array is
- printf("Sorted array is: \n");
- for (k = 0; k < nums; k++)
- {
- printf("%d ", arr[k]);
- }
- puts("\nThats it!\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement