Skip to content

Commit 162a62e

Browse files
authored
Add files via upload
1 parent 60f95da commit 162a62e

18 files changed

+1134
-0
lines changed

Sorting Technique/01 Bubble Sort.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
void swap(int* x, int* y) // function for swapping
5+
{
6+
int temp = *x;
7+
*x = *y;
8+
*y = temp;
9+
}
10+
11+
void Bubble(int A[], int n) // size of array // no of elements
12+
{
13+
int i, j, flag = 0; // flag is for adptive (Criteria)
14+
for (i = 0; i < n - 1; i++)
15+
{
16+
flag = 0;
17+
for (j = 0; j < n - i - 1; j++)
18+
{
19+
if (A[j] > A[j + 1])
20+
{
21+
swap(&A[j], &A[j + 1]); // if is greater then swap the elements
22+
flag = 1;
23+
}
24+
}
25+
if (flag == 0)
26+
break;
27+
28+
}
29+
}
30+
int main()
31+
{
32+
int A[20], i;
33+
int n = sizeof(A) / sizeof(int);
34+
scanf_s("%d", &n);
35+
for (i = 0; i < n; i++)
36+
{
37+
scanf_s("%d", & A[i]);
38+
}
39+
40+
Bubble(A, n); // callig the funtion
41+
42+
for (i = 0; i < n; i++)
43+
printf("%d ", A[i]);
44+
printf("\n");
45+
46+
return 0;
47+
}
48+
49+
// Bubble sort is Adptive: if the list is sorted it will use flag // Bubble sort is stable : in case of duplicate
50+
// time complexity is : min = o(n) - in the case of Adaptivity
51+
// : max = o(n2)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
template <class T>
6+
void Print(T& vec, int n, string s){
7+
cout << s << ": [" << flush;
8+
for (int i=0; i<n; i++){
9+
cout << vec[i] << flush;
10+
if (i < n-1){
11+
cout << ", " << flush;
12+
}
13+
}
14+
cout << "]" << endl;
15+
}
16+
17+
void swap(int* x, int* y){
18+
int temp = *x;
19+
*x = *y;
20+
*y = temp;
21+
}
22+
23+
void BubbleSort(int A[], int n){
24+
int flag = 0;
25+
for (int i=0; i<n-1; i++){
26+
for (int j=0; j<n-1-i; j++){
27+
if (A[j] > A[j+1]){
28+
swap(&A[j], &A[j+1]);
29+
flag = 1;
30+
}
31+
}
32+
if (flag == 0){
33+
return;
34+
}
35+
}
36+
}
37+
38+
int main() {
39+
40+
int A[] = {3, 7, 9, 10, 6, 5, 12, 4, 11, 2};
41+
int n = sizeof(A)/sizeof(A[0]);
42+
Print(A, n, "\t\tA");
43+
44+
BubbleSort(A, n);
45+
Print(A, n, " Sorted A");
46+
47+
return 0;
48+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <stdio.h>
2+
void swap(int* a, int* b)
3+
{
4+
int temp = *a;
5+
*a = *b;
6+
*b = temp;
7+
}
8+
void Insertionsort(int A[], int n)
9+
{
10+
int i, j, x;
11+
for (i = 1; i < n; i++) // this is for repetation of passes
12+
{
13+
j = i - 1; // set j as one before the last
14+
x = A[i]; // the elemets that we have to insert
15+
// go on shifting the elements as long as a[j] is greater then x and everytime reduce j by 1 ; j--
16+
while (j > -1 && A[j] > x) // shifting the elements
17+
{
18+
// when place is found to insert then
19+
A[j + 1] = A[j]; // everytime at the next location copy the elemets of j
20+
j--; // decrement j
21+
}
22+
A[j + 1] = x; // at end copy the element at appropriate position
23+
}
24+
25+
}
26+
int main()
27+
{
28+
int A[20],no,i;
29+
scanf_s("%d", &no);
30+
for (i = 0; i < no; i++)
31+
{
32+
scanf_s("%d", &A[i]);
33+
}
34+
Insertionsort(A, no);
35+
for (i = 0; i < no; i++)
36+
printf("%d ", A[i]);
37+
printf("\n");
38+
return 0;
39+
40+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
template <class T>
6+
void Print(T& vec, int n, string s){
7+
cout << s << ": [" << flush;
8+
for (int i=0; i<n; i++){
9+
cout << vec[i] << flush;
10+
if (i < n-1){
11+
cout << ", " << flush;
12+
}
13+
}
14+
cout << "]" << endl;
15+
}
16+
17+
void InsertionSort(int A[], int n){
18+
for (int i=1; i<n; i++){
19+
int j = i-1;
20+
int x = A[i];
21+
while (j>-1 && A[j] > x){
22+
A[j+1] = A[j];
23+
j--;
24+
}
25+
A[j+1] = x;
26+
}
27+
}
28+
29+
int main() {
30+
31+
int A[] = {19, 17, 15, 13, 11, 9, 7, 5, 3, 1};
32+
Print(A, sizeof(A)/sizeof(A[0]), " A");
33+
34+
InsertionSort(A, sizeof(A)/sizeof(A[0]));
35+
Print(A, sizeof(A)/sizeof(A[0]), "Sorted A");
36+
37+
return 0;
38+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <stdio.h>
2+
3+
void swap(int* a, int* b)
4+
{
5+
int temp = *a;
6+
*a = *b;
7+
*b = temp;
8+
}
9+
void selectionsort(int A[], int n)
10+
{
11+
int i, j, k; // for indexes
12+
for (i = 0; i < n - 1; i++) // for passes // in 1st pass we will get the smallest element
13+
{
14+
for (j = k = i; j < n; j++) // for checing all elements // keep i,j,k on 1st pos and strating moving j everytime and compare with k
15+
{
16+
// move j to next element and comp with k . if j is smaller then k then bring k at j..
17+
if (A[j] < A[k]) // if j is less then k then move k and we will
18+
k = j; // now k is pointing on the smallest elements
19+
}
20+
swap(&A[i], &A[k]); // now swap k with i then we will get the smallest elements at that position (i) // in this only one swap will happen
21+
}
22+
}
23+
int main()
24+
{
25+
int no, i, A[20];
26+
no = sizeof(A) / sizeof(int);
27+
scanf_s("%d", &no);
28+
for (i = 0; i < no; i++)
29+
{
30+
scanf_s("%d", &A[i]);
31+
}
32+
selectionsort(A, no);
33+
for (i = 0; i < no; i++)
34+
{
35+
printf("%d ", A[i]);
36+
}
37+
printf("\n");
38+
return 0;
39+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
template <class T>
6+
void Print(T& vec, int n, string s){
7+
cout << s << ": [" << flush;
8+
for (int i=0; i<n; i++){
9+
cout << vec[i] << flush;
10+
if (i < n-1){
11+
cout << ", " << flush;
12+
}
13+
}
14+
cout << "]" << endl;
15+
}
16+
17+
void swap(int* x, int* y){
18+
int temp = *x;
19+
*x = *y;
20+
*y = temp;
21+
}
22+
23+
void SelectionSort(int A[], int n){
24+
for (int i=0; i<n-1; i++){
25+
int j;
26+
int k;
27+
for (j=k=i; j<n; j++){
28+
if (A[j] < A[k]){
29+
k = j;
30+
}
31+
}
32+
swap(&A[i], &A[k]);
33+
}
34+
}
35+
36+
int main() {
37+
38+
int A[] = {3, 7, 9, 10, 6, 5, 12, 4, 11, 2};
39+
int n = sizeof(A)/sizeof(A[0]);
40+
Print(A, n, "\t\tA");
41+
42+
SelectionSort(A, n);
43+
Print(A, n, " Sorted A");
44+
45+
return 0;
46+
}

Sorting Technique/07 Mergesort.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// sorting.cpp : This file contains the 'main' function. Program execution begins and ends there.
2+
//
3+
#include<stdio.h>
4+
5+
void Merge(int A[], int l, int mid, int h) // this is for two array inside one array
6+
{
7+
int i = l, j = mid + 1, k = l;
8+
int B[100];
9+
10+
while (i <= mid && j <= h)
11+
{
12+
if (A[i] < A[j])
13+
B[k++] = A[i++]; // copy element from a to b
14+
else
15+
B[k++] = A[j++]; // copy element from another side of a to b
16+
}
17+
for (; i <= mid; i++)
18+
B[k++] = A[i];
19+
for (; j <= h; j++)
20+
B[k++] = A[j];
21+
22+
for (i = l; i <= h; i++) // this is for copy all the elements from B to A( back )
23+
A[i] = B[i];
24+
}
25+
void MergeSort(int A[], int l, int h)
26+
{
27+
int mid;
28+
if (l < h)
29+
{
30+
mid = (l + h) / 2;
31+
MergeSort(A, l, mid);
32+
MergeSort(A, mid + 1, h);
33+
Merge(A, l, mid, h);
34+
35+
}
36+
}
37+
int main()
38+
{
39+
int A[] = { 11,13,7,12,16,9,24,5,10,3 }, n = 10, i;
40+
41+
MergeSort(A, 0, 9);
42+
43+
for (i = 0; i < 10; i++)
44+
printf("%d ", A[i]);
45+
printf("\n");
46+
47+
return 0;
48+
}
49+
// time complexity = o(m+n)

0 commit comments

Comments
 (0)