0% found this document useful (0 votes)
27 views

Sorting Algorithm

The document discusses different sorting algorithms. It includes code snippets for insertion sort, finding the minimum element in a rotated array, and recursively counting the number of occurrences of an element in an array.

Uploaded by

HuZaM Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Sorting Algorithm

The document discusses different sorting algorithms. It includes code snippets for insertion sort, finding the minimum element in a rotated array, and recursively counting the number of occurrences of an element in an array.

Uploaded by

HuZaM Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Sorting Algorithm

int leastDifference(int a[], int n) {


int temp;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp =a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
int si=0;
int diff= a[si+1]-a[si];

return diff;
}

void insertion(int A[], int size){


int k,j;
for(int i = 1; i<size; i++) {
k = A[i];
j = i;
while(j > 0 && A[j-1]>k) {
A[j] = A[j-1];
j--;
}
A[j] = k;
}
}

int FindK(int arr[], int low, int high)


{
//agr array rotated ni hoi ho gi to ye value wapis ho gi
if (high < low)
{
return 0;
}

// If there is only one element left


if (high == low)
{
return low;
}

// Find mid
int mid = low + (high - low)/2; //ye is liye ta k agr negative value a
jay to bhi mid easily find ho jay agr smjh na ay to ye code bhi likh skty ha mid find
krny k liye ==>> /*(low + high)/2;*/

// mid sy next value greater ha ya ni find krna


if (mid < high && arr[mid+1] < arr[mid])
{
return (mid+1);
}

// mid ki value minimum ha ya ni


if (mid > low && arr[mid] < arr[mid - 1])
{
return mid;
}

//mid sy left wali array ma minimum value check krna


if (arr[high] > arr[mid])
{
return FindK(arr, low, mid-1);
}
//mid sy right wali array ma minimum value check krna
return FindK(arr, mid+1, high);
}

int count(int* arr, int start, int end, int k){


if(start==end)
{
return 0;
}
if(arr[start]==k){
return start;
}
return count(arr,start+1,end,k);
}

You might also like