Skip to content

Commit 5ed72a8

Browse files
authored
Add files via upload
1 parent 0e302f6 commit 5ed72a8

File tree

5 files changed

+279
-0
lines changed

5 files changed

+279
-0
lines changed

Bubble sort.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include<iostream>
2+
3+
using namespace std;
4+
5+
6+
int main ()
7+
8+
{
9+
int array[15],i, j, n;
10+
11+
cout<<"input the size of array : ";
12+
cin>>n;
13+
14+
// for getting the input form the user
15+
cout<<"Input the unsorted element : ";
16+
for (i=0;i<n;i++)
17+
{
18+
cin>> array[i];
19+
}
20+
21+
// for printing the given value
22+
23+
cout<<"The given unsorted element is : ";
24+
for (i=0;i<n;i++)
25+
{
26+
cout<< array[i]<< " ";
27+
}
28+
cout<<endl;
29+
// for sorting the element
30+
31+
for (i=0;i<n;i++)
32+
{
33+
for( int j=i+1;j<n;j++)
34+
{
35+
if(array[i]>array[j])
36+
{
37+
int tem=array[i];
38+
array[i]=array[j];
39+
array[j]=tem;
40+
}
41+
}
42+
}
43+
44+
// FOR PRINTING THE SORTED ELEMENTS
45+
cout<<"The sorted element Is : ";
46+
47+
for(i=0;i<n;i++)
48+
{
49+
cout<<array[i]<<" ";
50+
}
51+
52+
53+
54+
return 0;
55+
}

insertion sort 1.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int i , array[5]={3,8,6,77,9}, n=5;
5+
6+
void InsertionSort ()
7+
{
8+
for(i=1;i<n;i++)
9+
{
10+
int key=array[i];
11+
int j=i-i;
12+
13+
while (j >= 0 && array[j] > key)
14+
{
15+
array[j+1]=array[j];
16+
j--;
17+
}
18+
array[j+i]=key;
19+
}
20+
}
21+
22+
23+
void print()
24+
{
25+
for(i=0;i<n;i++)
26+
{
27+
cout<<array[i]<<endl;
28+
}
29+
}
30+
31+
32+
33+
int main ()
34+
{
35+
cout<<"Before sorted: "<<endl;
36+
print();
37+
InsertionSort();
38+
cout<<"After sorted: "<<endl;
39+
print();
40+
return 0;
41+
42+
43+
}

linier search in array.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
3+
#include<iostream>
4+
5+
using namespace std;
6+
7+
8+
int main ()
9+
10+
11+
{
12+
int array[10], i, x,n;
13+
14+
cout<<"Input the size of array : ";
15+
cin>> n;
16+
cout<<"Input the element of the array :" ;
17+
18+
for(i=0;i<n;i++)
19+
{
20+
cin>>array[i];
21+
}
22+
// print the given in put
23+
24+
for (i=0;i<n;i++)
25+
{
26+
cout<<array[i]<<endl;
27+
}
28+
29+
30+
// for search a value_type
31+
32+
cout<<"Input a value you want to search:"<<endl;
33+
cin>> x;
34+
35+
36+
for (i=0;i<n;i++)
37+
{
38+
if(array[i]==x)
39+
{
40+
cout<<"The value is found at the index : " <<i;
41+
42+
break;
43+
}
44+
else
45+
{
46+
cout<<"The value is not found in the array"<<endl;
47+
break;
48+
}
49+
}
50+
51+
52+
return 0;
53+
}

mergesort.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <iostream> // Include the input-output stream header
2+
3+
using namespace std; // Use the standard namespace
4+
5+
// Function to merge two subarrays of arr[].
6+
// First subarray is arr[left..mid]
7+
// Second subarray is arr[mid+1..right]
8+
void merge(int arr[], int left, int mid, int right) {
9+
int n1 = mid - left + 1; // Calculate the size of the first subarray
10+
int n2 = right - mid; // Calculate the size of the second subarray
11+
12+
int L[n1], R[n2]; // Create temporary arrays for the two subarrays
13+
14+
// Copy data to temporary array L[] from the left subarray
15+
for (int i = 0; i < n1; i++)
16+
L[i] = arr[left + i];
17+
18+
// Copy data to temporary array R[] from the right subarray
19+
for (int j = 0; j < n2; j++)
20+
R[j] = arr[mid + 1 + j];
21+
22+
// Initial indexes for the left (i), right (j) subarrays and merged subarray (k)
23+
int i = 0, j = 0, k = left;
24+
25+
// Merge the temporary arrays back into arr[left..right]
26+
while (i < n1 && j < n2) { // Continue while there are elements in both subarrays
27+
if (L[i] <= R[j]) { // If the current element of L[] is smaller or equal to R[]
28+
arr[k] = L[i]; // Place L[i] into the merged array
29+
i++; // Move to the next element in L[]
30+
} else {
31+
arr[k] = R[j]; // Place R[j] into the merged array
32+
j++; // Move to the next element in R[]
33+
}
34+
k++; // Move to the next position in the merged array
35+
}
36+
37+
// Copy any remaining elements of L[] into arr[], if any
38+
while (i < n1) {
39+
arr[k] = L[i];
40+
i++;
41+
k++;
42+
}
43+
44+
// Copy any remaining elements of R[] into arr[], if any
45+
while (j < n2) {
46+
arr[k] = R[j];
47+
j++;
48+
k++;
49+
}
50+
}
51+
52+
// Function to implement Merge Sort on arr[] from index 'left' to 'right'
53+
void mergeSort(int arr[], int left, int right) {
54+
if (left < right) { // Check if the array has more than one element
55+
int mid = (left +right) / 2; // Calculate the middle point to split the array
56+
57+
// Recursively sort the first half
58+
mergeSort(arr, left, mid);
59+
60+
// Recursively sort the second half
61+
mergeSort(arr, mid + 1, right);
62+
63+
// Merge the two sorted halves
64+
merge(arr, left, mid, right);
65+
}
66+
}
67+
68+
int main() {
69+
int arr[] = {38, 27, 43, 3, 9, 82,81, 10}; // Initialize the array to be sorted
70+
int arr_size = sizeof(arr) / sizeof(arr[0]); // Calculate the number of elements in the array
71+
72+
mergeSort(arr, 0, arr_size - 1); // Call mergeSort on the entire array
73+
74+
// Output the sorted array
75+
cout << "Sorted array: ";
76+
for (int i = 0; i < arr_size; i++) // Iterate through the sorted array
77+
cout << arr[i] << " "; // Print each element followed by a space
78+
cout << endl; // Print a newline at the end
79+
80+
return 0; // Return 0 to indicate successful execution
81+
}
82+

selection sort.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void selectionSort(int arr[], int n) {
5+
int i, j, minIndex;
6+
7+
// One by one move the boundary of the unsorted subarray
8+
for (i = 0; i < n-1; i++) {
9+
// Find the minimum element in the unsorted array
10+
minIndex = i;
11+
for (j = i+1; j < n; j++) {
12+
if (arr[j] < arr[minIndex]) {
13+
minIndex = j;
14+
}
15+
}
16+
17+
// Swap the found minimum element with the first element
18+
int temp = arr[minIndex];
19+
arr[minIndex] = arr[i];
20+
arr[i] = temp;
21+
}
22+
}
23+
24+
// Function to print the array
25+
void printArray(int arr[], int ) {
26+
for (int i = 0; i <n; i++) {
27+
cout << arr[i] << " ";
28+
}
29+
cout << endl;
30+
}
31+
32+
int main() {
33+
int arr[] = {64, 25, 12, 22, 11};
34+
int n = sizeof(arr)/sizeof(arr[0]);
35+
36+
cout << "Unsorted array: \n";
37+
printArray(arr, n);
38+
39+
selectionSort(arr, n);
40+
41+
cout << "Sorted array: \n";
42+
printArray(arr, n);
43+
44+
return 0;
45+
}
46+

0 commit comments

Comments
 (0)