Searching and Sorting of an Array in C++

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Program 1

// Searching and Sorting 
#include<iostream>
using namespace std;
int main()
{
     system("cls");
      int arr[100],n,i,s,j,temp;
      bool flag=false;
     read: cout<<"\nEnter limit of array: ";
      cin>>n;
      if(n>100 || n<=0)
      {
          cout<<"\n Invalid limit enter between(1-100)";
          goto  read;
      }
      cout<<"Enter "<<n<<" elements in array: ";
      for(i=0;i<n;i++)
      cin>>arr[i]; 
      for(i=0;i<n;i++)          
      {
          for(j=i+1;j<n;j++)   
          {
             if(arr[j]<arr[i])
             {
                temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
             }
          }
      }
      cout<<"\nSorted elements : ";
      for(i=0;i<n;i++)
       cout<<endl<<arr[i];
    return 0;
}

/*
   a[0] 1 
   a[1] 2
   a[2] 5 
   a[3] 4 
   a[4] 3 
*/




// Searching 
// int main()
// {
//      system("cls");
//       int arr[100],n,i,s;
//       bool flag=false;
//      read: cout<<"\nEnter limit of array: ";
//       cin>>n;
//       if(n>100 || n<=0)
//       {
//           cout<<"\n Invalid limit enter between(1-100)";
//           goto  read;
//       }
//       cout<<"Enter "<<n<<" elements in array: ";
//       for(i=0;i<n;i++)
//       cin>>arr[i];
//      cout<<"Enter an element for search: ";
//      cin>>s;
//      for(i=0;i<n;i++)
//      {
//           if(arr[i]==s)
//           {
//             flag=true;
//             break;
//           }
//      }
//      if(flag==true)
//       cout<<"Searching success";
//      else
//        cout<<"Searching not success";
//     return 0;
// }

/*
   n=5
   s=78
   a[0] 34
   a[1] 23
   a[2] 78
   a[3] 77
   a[4]  90
*/

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *