Skip to main content

Sort n numbers in ascending order using pointers

C program to sort n numbers in ascending order using pointers


  #include <stdio.h>
  #include <stdlib.h>
  int main() {
        int i, j, n, tmp, *ptr;
        /* get the number of inputs from the user */
        printf("Enter number of inputs:");
        scanf("%d", &n);

        /* dynamic memory allocation for n elements */
        ptr = (int *)malloc(sizeof(int) * n);

        /* get the inputs from the user */
        printf("Enter your inputs:\n");
        for (i = 0; i < n; i++) {
                scanf("%d", ptr + i);
        }

        /* sort the given numbers in ascending order */
        for (i = 0; i < n - 1; i++) {
                for (j = i + 1; j < n; j++) {
                        if (*(ptr + i) > *(ptr + j)) {
                                tmp = *(ptr + i);
                                *(ptr + i) = *(ptr + j);
                                *(ptr + j) = tmp;
                        }
                }
        }

        /* prints the sorted numbers */
        printf("Output:\n");
        for (i = 0; i < n; i++) {
                printf("%d ", *(ptr + i));
        }
        printf("\n");

        /* release the dynamically allocated memory */
        free(ptr);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter number of inputs: 5
  Enter your inputs:
  99  29  34  14  25
  Output:
  14  25  29  34  99 


Comments

Popular posts from this blog

restorecrtmode example in c

Header file:     graphics.h Synopsis:        void restorecrtmode();       Description:       restorecrtmode() restores screen mode to text mode. restorecrtmode function in c graphics   #include <graphics.h>   #include <stdlib.h>   #include <stdio.h>   #include <conio.h>   int main(void) {         /* request auto detection */         int gd = DETECT, gmode, err;         int midx, midy;         /* initialize graphics and local variables */         initgraph(&gd, &gmode, "C:/TURBOC3/BGI");         /* read result of initialization */         err = graphresult();         if (err != grOk) {                 /* an error occurred */               ...

Array of structures

We can create array of structures similar to creating array of any primitive data types.   Below is the general form of declaration for array of structure. struct  <structure_name>  <array_name>[SIZE]; Consider the following example struct student { char name[32]; int age, rollno; }; struct student arr[2]; Here, arr is an array of 2 structure elements. Let us see how to initialize an array of structures. Method 1: struct student arr[2] = { {"Tom", 10, 101}, {"Jerry", 11, 102} }; Method 2: strcpy(arr[0].name, "Tom"); arr[0].age = 10; arr[0].rollno = 101; strcpy(arr[1].name, "Jerry"); arr[1].age = 11; arr[1].rollno = 102; Apart from the above, we are allowed to do partial initialization for structure elements in an array. Consider the following, struct student arr[2] = {{"Tom", 10, 101}, {"Jerry"}}; In the above example, we have done partial initialization for second element in the structure array...