Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Sunday, September 4, 2022

Sort an array of input ten elements in an ascending order in C programming


Sort an array of input ten elements in an ascending order in C programming






Introduction:


Sorting an array is a fundamental operation in computer programming, as it allows for easier search and organization of data. In C programming, sorting an array of ten elements in ascending order can be achieved through various methods such as bubble sort, selection sort, and insertion sort. In this article, we will explore these methods and provide step-by-step instructions on how to implement them in C programming.

Understanding Sorting Algorithms


Before we dive into the specifics of sorting an array in C programming, it's important to have a basic understanding of sorting algorithms. Sorting algorithms are procedures that arrange a set of elements in a specific order. The most common orders are ascending and descending.

There are many sorting algorithms available, each with its own advantages and disadvantages. Some of the most popular sorting algorithms include bubble sort, selection sort, insertion sort, quicksort, and mergesort.

Bubble Sort Algorithm

Bubble sort is a simple sorting algorithm that works by repeatedly swapping adjacent elements if they are in the wrong order. The algorithm takes its name from the way smaller elements "bubble" to the top of the list during each pass.

To sort an array of ten elements in ascending order using the bubble sort algorithm in C programming, follow these steps:Start by comparing the first two elements of the array.
If the first element is greater than the second element, swap them.
Move to the next pair of elements and repeat the process until the end of the array is reached.
Repeat the process for the entire array until no more swaps are needed.

Sample coding:

Here's a sample code implementation of the bubble sort algorithm in C programming:

#include<stdio.h>
#include<conio.h>
int main()
{
int a[10], i, j, temp;
printf("Enter number\n");
for (i=0;i<=9;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<=9;i++)
{
for(j=i+1;j<=9;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\n Sorted numbers\n");
for(i=0;i<=9;i++)
printf("%d\n",a[i]);
getch();
}

Explanation of the program:


This is a C program that sorts an array of 10 integers in ascending order using the bubble sort algorithm. Here's how the program works:

The program first declares an integer array a of size 10, along with other variables such as i, j, and temp.


The program then prompts the user to enter 10 integers, which are stored in the array a using a for loop and the scanf function.

The program then uses a nested for loop to perform the bubble sort algorithm. The outer loop runs from i=0 to i<=9, and the inner loop runs from j=i+1 to j<=9. Inside the inner loop, the program checks whether the element at index i is greater than the element at index j. If it is, the program swaps the elements using a temporary variable temp.

After the inner loop completes for each iteration of the outer loop, the largest element is "bubbled up" to the end of the array. This means that the largest element is now in its correct sorted position.

The outer loop continues for the remaining elements until the entire array is sorted in ascending order.

Finally, the sorted array is printed to the console using another for loop and the printf function.

The program waits for a key to be pressed before exiting, using the getch function.

Bubble sort is not the most efficient sorting algorithm, but it is simple and easy to understand. The nested loop in this program has a time complexity of O(n^2), which can be slow for large arrays. Selection sort or merge sort algorithms are more efficient for larger arrays.

Selection Sort Algorithm

Selection sort is another simple sorting algorithm that works by repeatedly selecting the smallest element from the unsorted portion of the array and placing it at the beginning of the array.

To sort an array of ten elements in ascending order using the selection sort algorithm in C programming, follow these steps:Find the smallest element in the unsorted portion of the array.
Swap the smallest element with the first element in the unsorted portion.
Move to the next element in the unsorted portion and repeat steps 1 and 2.
Repeat the process for the entire array until the array is sorted.

Here's a sample code implementation of the selection sort algorithm in C programming:


#include <stdio.h>
#include <conio.h>

void selectionSort(int arr[], int n);

int main() {
    int a[10], i;

    printf("Enter 10 numbers:\n");

    for (i = 0; i < 10; i++) {
        scanf("%d", &a[i]);
    }

    selectionSort(a, 10);

    printf("\nSorted numbers:\n");

    for (i = 0; i < 10; i++) {
        printf("%d\n", a[i]);
    }

    getch();
    return 0;
}

void selectionSort(int arr[], int n) {
    int i, j, min_idx;

    for (i = 0; i < n-1; i++) {
        min_idx = i;
        for (j = i+1; j < n; j++) {
            if (arr[j] < arr[min_idx]) {
                min_idx = j;
            }
        }
        int temp = arr[min_idx];
        arr[min_idx] = arr[i];
        arr[i] = temp;
    }
}



In the above program, we first define a function selectionSort that takes an array arr and its size n as input. This function implements the selection sort algorithm to sort the given array in ascending order.

We then call this function from the main function after taking the input of 10 integers from the user. The sorted array is then printed using a loop.

By using the selection sort algorithm, we can improve the performance of the program compared to the previous version which used a nested loop for sorting.

Output:




Conclusion

Sorting an array of ten elements in ascending order is a fundamental task in C programming. In this article, we have explored three different sorting algorithms – bubble sort, selection sort, and insertion sort – and provided step-by-step instructions on how to implement each one in C programming. Remember that the choice of sorting algorithm depends on several factors, so choose wisely based on your specific use case.

FAQs


1) What is an array? 
Ans: An array is a collection of elements of the same data type, stored in contiguous memory locations.


2) What is the difference between ascending and descending order? Ans: Ascending order means arranging elements from smallest to largest, while descending order means arranging elements from largest to smallest.

3) Can sorting algorithms be applied to different data types? 
Ans: Yes, sorting algorithms can be applied to different data types, but the implementation may differ based on the specific data type.

4) Are there other sorting algorithms besides the ones mentioned in this article? 
Ans: Yes, there are many other sorting algorithms available, each with its own advantages and disadvantages.

5) What is the time complexity of the bubble sort algorithm? 
Ans: The time complexity of the bubble sort algorithm is O(n^2).

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages