-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c193780
commit 1c6cd94
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include<iostream> | ||
#include<stdio.h> | ||
using namespace std; | ||
void sort(int arr[], int n) | ||
{ | ||
int writes = 0; | ||
for (int cycle_start = 0; cycle_start < n - 1; cycle_start++) { | ||
int item = arr[cycle_start]; | ||
int pos = cycle_start; | ||
for (int i = cycle_start + 1; i < n; i++) | ||
if (arr[i] < item) | ||
pos++; | ||
if (pos == cycle_start) | ||
continue; | ||
while (item == arr[pos]) | ||
pos += 1; | ||
|
||
if (pos != cycle_start) { | ||
swap(item, arr[pos]); | ||
writes++; | ||
} | ||
|
||
while (pos != cycle_start) { | ||
pos = cycle_start; | ||
|
||
for (int i = cycle_start + 1; i < n; i++) | ||
if (arr[i] < item) | ||
pos += 1; | ||
|
||
while (item == arr[pos]) | ||
pos += 1; | ||
|
||
if (item != arr[pos]) { | ||
swap(item, arr[pos]); | ||
writes++; | ||
} | ||
} | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
int f; | ||
cout<<"Enter length of array "; | ||
cin>>f; | ||
int arr[f]; | ||
cout<<"Enter array"<<endl; | ||
for(int i=0;i<f;i++){ | ||
cin>>arr[i]; | ||
} | ||
sort(arr, f); | ||
|
||
cout << "Sorted array: " << endl; | ||
for (int i = 0; i < f; i++) | ||
cout << arr[i] << " "; | ||
return 0; | ||
} | ||
|