• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Write a C Program to Check if Array is Sorted

Aniruddha Chaudhari/43803/2
C / C++Code

This question has been asked in many of the placement interviews. We can check an array is sorted or not by simply comparing all the elements with its next element.

There are two cases as per comparison between the element and its next element.

Case 1:

For all elements, if the element is equal to or less than its next element; we can conclude that the array is sorted in ascending order.

Case 2:

If it does not satisfy even for the single element, the array is not in ascending order.

This is one of the very important data structure interview coding questions.

Difficulty Level: Low

C Program to Check if Array is Sorted

Go through the following code to check array is sorted or not in ascending order.

#include<stdio.h>
#define nSize 10 //size of array
int main()
{
    int nArr[nSize]={0};
    int i=0;
    printf("Enter 10 Elements");
    for (i=0; i<10; i++)
        scanf("%d", &nArr[i]);
 
    for(i=0; i<nSize-1 ; i++) { if(nArr[i]>nArr[i+1])
        //if(nArr[i]<nArr[i+1]) to check descending order
        {
            printf("Array is not sorted in assenting order");
            return;
        }
    }
    printf("Array is sorted in assenting order");
}

Output :

Case 1:

input : 10 12 24 35 56 66 77 78 89 90
Output : Array is sorted in ascending order

Case 2:

input : 10 12 5 35 7 66 77 78 89 90
Output : Array is not sorted in ascending order

Note: To check the array is sorted or not in descending order, just use condition if(nArr[i]<nArr[i+1]) in the above code.

Time complexity:

As we are comparing each element at once with its next element, the time complexity is O(n).

You can also sort the given array. And then compare the original array with the output array.

If the elements in both arrays are the same and in the same order, the given array is sorted.

You can use any sorting algorithm to sort the given array. Like…

  • Selection Sort
  • Bubble Sort
  • Quick Sort

Do you know any other C program to check if array is sorted? Please share it in the comment?

C-Arraycppsorting
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    Mealy Ved
    March 7, 2020 at 9:36 pm

    why we write for(i=0; i<nSize-1 ; i++)

    • Reply
      Aniruddha Chaudhari
      March 8, 2020 at 6:41 pm

      I guess you have confusion with nSize-1 and why it is not nSize. In our logic, every time in the loop, we are comparing the current element with the next element. We only have to traverse up to the second last element where the second last element will be compared with the last element. I hope it is clear.

Leave a Reply Cancel reply

C Programming

  1. C- Introduction
  2. C- Compile & Execute Program
  3. C- Data Types
  4. C- if-else statement
  5. C- While, do-while, for loop
  6. C- Array
  7. C- Function (Types/Call)
  8. C- strlen() vs sizeof()
  9. C- Nested Switch Statement
  10. C- Recursion
  11. C- Dynamic Programming
  12. C- Storage Classes
  13. C- Creating Header File
  14. C- Null Pointer
  15. C- Stack and Queue
  16. C- Implement Stack using Array
  17. C- Implement Linked List in C
  18. C- File Handling
  19. C- Makefile Tutorial

Object Oriented Concepts in C++

  • C++: C vs OOPs Language
  • C++: Introduction to OOPs Concepts
  • C++: Inheritance

Sorting Algorithms

  • Different Types of Sorting Algo
  • Selection Sort
  • Bubble Sort
  • Quick Sort

Programming for Practice

  1. Online C/C++ Compiler

String Handling:

  1. Remove White Spaces from String
  2. Implement strstr Function in C
  3. Convert String to Int – atoi()
  4. Check if String is Palindrome
  5. Check if Two Strings are Anagram
  6. Split String in using strtok_r()
  7. Undefined reference to strrev

Array:

  1. Check if Array is Sorted

Bit Manipulation:

  1. Count Number of 1’s in Binary

Linked List:

  1. Reverse a Linked List Elements

Number System:

  1. Program to Find 2’s Complement
  2. Convert Decimal to Binary in C

Tricky Questions:

  1. Add Two Numbers without Operator
  2. Find Next Greater Number
  3. Swap values without temp variable
  4. Print 1 to 100 without Loop

Interview Coding Questions

  • 50+ Interview Coding Questions

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard