LATEST

Searching and sorting arrays in C / C++

In the previous tutorial, we learned about using arrays in C / C++. In this tutorial, we will learn how to searching and sorting arrays in C / C++.

These are two very important operations when working with arrays in C / C++. So, let's find out carefully.

So, how to search and sort arrays in C / C++? Let's get started !!!

Searching and sorting arrays in C / C++

Searching for an element in an array is how we determine if that element exists in the array. If it exists, return the position it is in the array.

Example: Suppose we have array number[] = {1, 2, 3, 4, 5}, now look for element x = 3 whether exists in array or not.

Output: Returns the element x = 3 that exists in the array and it is at position 2.

Sorting an array is when we change the position of the elements so that their values are ascending or descending.

Example: Let's say we have array number[] = {1, 5, 2, 7, 4}. Now do an ascending array sort.

Output: number[] = {1, 2, 4, 5, 7}.

Searching for an element in an array C / C++

To search for an element x in an array, we traverse the elements in the array and compare them with the value x. If there exists an element equal to the value x, then conclude x exists in the array.

For example: Searching for an element in an array C / C++

C++ programming:

#include <iostream>
using namespace std;

void show_array(int array[], int length){
    for(short i = 0; i < length; i++)  cout << array[i] <<' ';   
    cout << endl;
}
 
void find_element_index(const int* array, size_t size, int x){
    int result[100], count = 0;
 
    for (size_t i = 0; i < size - 1; ++i) {
        if (array[i] == x) {
            result[count] = i;
            ++count;
        }
    }
    cout << "Element "<< x <<" appear in position: ";
    show_array(result, count);
}

int main() {
  int n, x;
  do{
    cout<<"Enter the size of the array: ";
    cin>>n;
    if(n <= 0) cout<<"Please enter the size of the array greater than 0 !!!\n";
  }while(n <= 0);
   
  int arr[n];
  for(int i = 0; i <= n - 1; i++){
    cout<<"Enter the element "<<"[" <<i<<"]: ";
    cin>>arr[i];
  }

  cout<<"Elements in array is: ";
  for(int i = 0; i <= n - 1; i++){
    cout<<arr[i]<<" ";
  }

  cout<<"\nEnter the search element: ";
  cin>>x;

  find_element_index(arr,n,x);

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>

void show_array(int array[], int length){
    for(short i = 0; i < length; i++)  printf("%d ",array[i]);   
    printf("\n");
}
 
void find_element_index(const int* array, size_t size, int x){
    int result[100], count = 0;
 
    for (size_t i = 0; i < size - 1; ++i) {
        if (array[i] == x) {
            result[count] = i;
            ++count;
        }
    }
    printf("Element %d appear in position: ",x);
    show_array(result, count);
}

int main(void) {
  int n, x;
  do{
    printf("Enter the size of the array: ");
    scanf("%d", &n);
    if(n <= 0) printf("Please enter the size of the array greater than 0 !!!\n");
  }while(n <= 0);
   
  int arr[n];
  for(int i = 0; i <= n - 1; i++){
    printf("Enter the element [%d]: ",i);
    scanf("%d", &arr[i]);
  }

  printf("Elements in array is: ");
  for(int i = 0; i <= n - 1; i++){
    printf("%d ",arr[i]);
  }

  printf("\nEnter the search element: ");
  scanf("%d", &x);;

  find_element_index(arr,n,x);

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

sort 01 PNG

Sorting arrays in C / C++

When sorting an array, we have two ways to sort:

  • Sort in ascending order.
  • Sort in descending order.

Now we will learn each sort with the help of examples.

Example: Sort in ascending order.

C++ programming:

#include <iostream>
using namespace std;

void sort_numbers_ascending(int number[], int count)
{
   int temp, i, j, k;
   for (j = 0; j < count; ++j)
   {
      for (k = j + 1; k < count; ++k)
      {
         if (number[j] > number[k])
         {
            temp = number[j];
            number[j] = number[k];
            number[k] = temp;
         }
      }
   }
   cout<<"\nArray after sorting ascending: ";
   for (i = 0; i < count; ++i)
      cout<< number[i]<< " ";
}

int main() {
  int n;
  do{
    cout<<"Enter the size of the array: ";
    cin>>n;
    if(n <= 0) cout<<"Please enter the size of the array greater than 0 !!!\n";
  }while(n <= 0);
   
  int arr[n];
  for(int i = 0; i <= n - 1; i++){
    cout<<"Enter the element "<<"[" <<i<<"]: ";
    cin>>arr[i];
  }

  cout<<"Elements in array is: ";
  for(int i = 0; i <= n - 1; i++){
    cout<<arr[i]<<" ";
  }

  sort_numbers_ascending(arr, n);

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>

void sort_numbers_ascending(int number[], int count)
{
   int temp, i, j, k;
   for (j = 0; j < count; ++j)
   {
      for (k = j + 1; k < count; ++k)
      {
         if (number[j] > number[k])
         {
            temp = number[j];
            number[j] = number[k];
            number[k] = temp;
         }
      }
   }
   printf("\nArray after sorting ascending: ");
   for (i = 0; i < count; ++i)
      printf("%d ", number[i]);
}

int main(void) {
  int n;
  do{
    printf("Enter the size of the array: ");
    scanf("%d", &n);
    if(n <= 0) printf("Please enter the size of the array greater than 0 !!!\n");
  }while(n <= 0);
   
  int arr[n];
  for(int i = 0; i <= n - 1; i++){
    printf("Enter the element [%d]: ",i);
    scanf("%d", &arr[i]);
  }

  printf("Elements in array is: ");
  for(int i = 0; i <= n - 1; i++){
    printf("%d ",arr[i]);
  }

  sort_numbers_ascending(arr, n);

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

sort 02 PNG

Example: Sort in descending order.

C++ programming:

#include <iostream>
using namespace std;

void sort_numbers_descending(int number[], int count)
{
   int temp, i, j, k;
   for (j = 0; j < count; ++j)
   {
      for (k = j + 1; k < count; ++k)
      {
         if (number[j] < number[k])
         {
            temp = number[j];
            number[j] = number[k];
            number[k] = temp;
         }
      }
   }
   cout<<"\nArray after sorting descending: ";
   for (i = 0; i < count; ++i)
      cout<< number[i]<< " ";
}

int main() {
  int n;
  do{
    cout<<"Enter the size of the array: ";
    cin>>n;
    if(n <= 0) cout<<"Please enter the size of the array greater than 0 !!!\n";
  }while(n <= 0);
   
  int arr[n];
  for(int i = 0; i <= n - 1; i++){
    cout<<"Enter the element "<<"[" <<i<<"]: ";
    cin>>arr[i];
  }

  cout<<"Elements in array is: ";
  for(int i = 0; i <= n - 1; i++){
    cout<<arr[i]<<" ";
  }

  sort_numbers_descending(arr, n);

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>

void sort_numbers_descending(int number[], int count)
{
   int temp, i, j, k;
   for (j = 0; j < count; ++j)
   {
      for (k = j + 1; k < count; ++k)
      {
         if (number[j] < number[k])
         {
            temp = number[j];
            number[j] = number[k];
            number[k] = temp;
         }
      }
   }
   printf("\nArray after sorting descending: ");
   for (i = 0; i < count; ++i)
      printf("%d ", number[i]);
}

int main(void) {
  int n;
  do{
    printf("Enter the size of the array: ");
    scanf("%d", &n);
    if(n <= 0) printf("Please enter the size of the array greater than 0 !!!\n");
  }while(n <= 0);
   
  int arr[n];
  for(int i = 0; i <= n - 1; i++){
    printf("Enter the element [%d]: ",i);
    scanf("%d", &arr[i]);
  }

  printf("Elements in array is: ");
  for(int i = 0; i <= n - 1; i++){
    printf("%d ",arr[i]);
  }

  sort_numbers_descending(arr, n);

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

sort 03 PNG

Cùng chuyên mục:

How to use strings in C / C++

How to use strings in C / C++

How to use multidimensional Arrays in C / C++

How to use multidimensional Arrays in C / C++

How to use arrays in C / C++

How to use arrays in C / C++

How to use goto statement in C / C++

How to use goto statement in C / C++

How to use switch...case statement in C / C++

How to use switch...case statement in C / C++

How to use continue statement in C / C++

How to use continue statement in C / C++

How to use break statement in C / C++

How to use break statement in C / C++

How to use if - else statement in C / C++

How to use if - else statement in C / C++

How to use Binary Search Tree (BST) in C / C++

How to use Binary Search Tree (BST) in C / C++

How to use do while loop in C / C++

How to use do while loop in C / C++

How to use while loop in C / C++

How to use while loop in C / C++

How to use for loop in C / C++

How to use for loop in C / C++

Top