Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example program
- #include <iostream>
- #include <string>
- using namespace std;
- void display(int A[], int size){
- for (int i=0; i< size; ++i)
- cout << A[i] << " " ;
- cout << endl;
- }
- void bubblesort(int A[], int size) {
- for (int pass=1; pass < size; ++pass) {
- for (int i= 0; i < size-pass; ++i) {
- if (A[i] > A[i+1]){
- int temp = A[i];
- A[i] = A[i+1];
- A[i+1] = temp;}}}
- }
- void insertionsort(int A[], int size){
- // repeatedly insert the ith element into proper position
- // loop invariant: the subarray A from 0 to i-1 is sorted
- for (int i = 1; i < size; i++){
- int j = i;
- while (j > 0 && A[j] < A[j-1]){
- int temp = A[j];
- A[j] = A[j-1];
- A[j-1] = temp;
- j--;
- }
- }
- }
- int main() {
- int A[] = {2,4,6,8,1,3,5,7};
- display(A,8);
- bubblesort(A,8);
- display(A,8);
- int C[] = {2,4,6,8,1,3,5,7};
- display(C,8);
- insertionsort(C,8);
- display(C,8);
- }
Advertisement
Add Comment
Please, Sign In to add comment