You are on page 1of 11

/* Simple Insertion Sort Program in C++*/

/* Data Structure C++ Programs,C++ Array Examples */

#include <iostream>
#include<conio.h>
#include<stdlib.h>

#define MAX_SIZE 5

using namespace std;

void insertion(int[]);

int main() {
int arr_sort[MAX_SIZE], i, j, a, t;

cout << "Simple C++ Insertion Sort Example - Array\n";


cout << "\nEnter " << MAX_SIZE << " Elements for Sorting : " << endl;
for (i = 0; i < MAX_SIZE; i++)
cin >> arr_sort[i];

cout << "\nYour Data :";


for (i = 0; i < MAX_SIZE; i++) {
cout << "\t" << arr_sort[i];
}

for (i = 1; i < MAX_SIZE; i++) {


t = arr_sort[i];
j = i - 1;

while (j >= 0 && arr_sort[j] > t) {


arr_sort[j + 1] = arr_sort[j];
j = j - 1;
}
arr_sort[j + 1] = t;

cout << "\nIteration : " << i;


for (a = 0; a < MAX_SIZE; a++) {
cout << "\t" << arr_sort[a];
}
}

cout << "\n\nSorted Data :";


for (i = 0; i < MAX_SIZE; i++) {
cout << "\t" << arr_sort[i];
}

// getch();
return 0;
}

Out put
/* Simple Selection Sort Program Using Array in C++*/
/* Data Structure C++ Programs,C++ Array Examples */

#include <iostream>
#include<conio.h>
#include<stdlib.h>

#define MAX_SIZE 5

using namespace std;

int main() {
int arr_sort[MAX_SIZE], i, j, a, t, p;

cout << "Simple C++ Selection Sort Example - Array\n";


cout << "\nEnter " << MAX_SIZE << " Elements for Sorting : " << endl;
for (i = 0; i < MAX_SIZE; i++)
cin >> arr_sort[i];

cout << "\nYour Data :";


for (i = 0; i < MAX_SIZE; i++) {
cout << "\t" << arr_sort[i];
}

for (i = 0; i < MAX_SIZE; i++) {


p = i;
for (j = i; j < MAX_SIZE; j++) {
if (arr_sort[p] > arr_sort[j])
p = j;
}

if (p != 1) {
//Swapping Values
t = arr_sort[i];
arr_sort[i] = arr_sort[p];
arr_sort[p] = t;
}
cout << "\nIteration : " << i;
for (a = 0; a < MAX_SIZE; a++) {
cout << "\t" << arr_sort[a];
}
}

cout << "\n\nSorted Data :";


for (i = 0; i < MAX_SIZE; i++) {
cout << "\t" << arr_sort[i];
}

return 0;
}

Out put
/* Simple Bubble Sort Program Using Array in C++*/
/* Data Structure C++ Programs,Array C++ Programs */

#include <iostream>
#include<conio.h>
#include<stdlib.h>

#define MAX_SIZE 5

using namespace std;

int main() {
int arr_sort[MAX_SIZE], i, j, a, t;

cout << "Simple C++ Bubble Sort Example - Array\n";


cout << "\nEnter " << MAX_SIZE << " Elements for Sorting : " << endl;
for (i = 0; i < MAX_SIZE; i++)
cin >> arr_sort[i];
cout << "\nYour Data :";
for (i = 0; i < MAX_SIZE; i++) {
cout << "\t" << arr_sort[i];
}

for (i = 1; i < MAX_SIZE; i++) {


for (j = 0; j < MAX_SIZE - 1; j++) {
if (arr_sort[j] > arr_sort[j + 1]) {
//Swapping Values
t = arr_sort[j];
arr_sort[j] = arr_sort[j + 1];
arr_sort[j + 1] = t;
}
}

cout << "\nIteration : " << i;


for (a = 0; a < MAX_SIZE; a++) {
cout << "\t" << arr_sort[a];
}
}

cout << "\n\nSorted Data :";


for (i = 0; i < MAX_SIZE; i++) {
cout << "\t" << arr_sort[i];
}
return 0;
}

Out put
/* Simple Linear Search Program in C++*/
/* Data Structure C++ Programs,C++ Array Examples */

#include <iostream>
#include<conio.h>
#include<stdlib.h>

#define MAX_SIZE 5

using namespace std;

int main() {
int arr_search[MAX_SIZE], i, element;

cout << "Simple C++ Linear Search Example - Array\n";


cout << "\nEnter " << MAX_SIZE << " Elements for Searching : " << endl;
for (i = 0; i < MAX_SIZE; i++)
cin >> arr_search[i];
cout << "\nYour Data :";
for (i = 0; i < MAX_SIZE; i++) {
cout << "\t" << arr_search[i];
}

cout << "\nEnter Element to Search : ";


cin>>element;

/* for : Check elements one by one - Linear */


for (i = 0; i < MAX_SIZE; i++) {
/* If for Check element found or not */
if (arr_search[i] == element) {
cout << "\nLinear Search : Element : " << element << " : Found :
Position : " << i + 1 << ".\n";
break;
}
}

if (i == MAX_SIZE)
cout << "\nSearch Element : " << element << " : Not Found \n";

return 0;
}

Out put
/* Simple Binary Search Program Using Functions in C++*/
/* Data Structure C++ Programs,C++ Array Examples */

#include <iostream>
#include<conio.h>
#include<stdlib.h>

#define MAX_SIZE 5

using namespace std;

int main() {
int arr_search[MAX_SIZE], i, element;
int f = 0, r = MAX_SIZE, mid;

cout << "Simple C++ Binary Search Example - Array\n";


cout << "\nEnter " << MAX_SIZE << " Elements for Searching : " << endl;
for (i = 0; i < MAX_SIZE; i++)
cin >> arr_search[i];

cout << "\nYour Data :";


for (i = 0; i < MAX_SIZE; i++) {
cout << "\t" << arr_search[i];
}

cout << "\nEnter Element to Search : ";


cin>>element;

while (f <= r) {
mid = (f + r) / 2;

if (arr_search[mid] == element) {
cout << "\nSearch Element : " << element << " : Found : Position :
" << mid + 1 << ".\n";
break;
} else if (arr_search[mid] < element)
f = mid + 1;
else
r = mid - 1;
}

if (f > r)
cout << "\nSearch Element : " << element << " : Not Found \n";

return 0;
}

Out put

You might also like