0% found this document useful (0 votes)
8 views13 pages

4 Bubble Sorting

The document provides an overview of sorting algorithms, specifically Bubble Sort and Insertion Sort. It details the processes and steps involved in each algorithm, including example arrays and corresponding code implementations in C. The Bubble Sort algorithm repeatedly compares and swaps adjacent elements, while the Insertion Sort algorithm places each element in its proper position in a sorted array.

Uploaded by

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

4 Bubble Sorting

The document provides an overview of sorting algorithms, specifically Bubble Sort and Insertion Sort. It details the processes and steps involved in each algorithm, including example arrays and corresponding code implementations in C. The Bubble Sort algorithm repeatedly compares and swaps adjacent elements, while the Insertion Sort algorithm places each element in its proper position in a sorted array.

Uploaded by

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

SORTING

SORTING IS THE PROCESS OF ARRANGING THE ELEMENTS IN


A PARTICULAR ORDER (ASCENDING OR DESCENDING)
BUBBLE SORT

• IN PASS 1, A[0] IS COMPARED WITH A[1], A[1] IS COMPARED WITH A[2], A[2] IS
COMPARED WITH A[3] AND SO ON. AT THE END OF PASS 1, THE LARGEST
ELEMENT OF THE LIST IS PLACED AT THE HIGHEST INDEX OF THE LIST.
• IN PASS 2, A[0] IS COMPARED WITH A[1], A[1] IS COMPARED WITH A[2] AND SO
ON. AT THE END OF PASS 2 THE SECOND LARGEST ELEMENT OF THE LIST IS
PLACED AT THE SECOND HIGHEST INDEX OF THE LIST.
• IN PASS N-1, A[0] IS COMPARED WITH A[1], A[1] IS COMPARED WITH A[2] AND
SO ON. AT THE END OF THIS PASS. THE SMALLEST ELEMENT OF THE LIST IS
PLACED AT THE FIRST INDEX OF THE LIST.
WORKING

10, 9, 7, 101, 23, 44, 12, 78, 34, 23


9 7 10 23 44 12 78 34 23 101 ------PASS1
7 9 10 23 12 44 34 23 78 101 ---------------
PASS 2
7 9 10 12 23 34 23 44 78 101 --------------
PASS 3
7 9 10 12 23 23 34 44
ALGORITHM :

• STEP 1: REPEAT STEP 2 FOR I = 0 TO


N-1
• STEP 2: REPEAT FOR J = I + 1 TO N -
I
• STEP 3: IF A[J] > A[I]
SWAP A[J] AND A[I]
[END OF INNER LOOP]
[END OF OUTER LOOP
• STEP 4: EXIT
BUBBLE SORT PROGRAM
• #include<stdio.h>
• main()
• {
• int i,n,j,a[30],temp;
• printf("\n enter the number of elements:");
• scanf("%d",&n);
• printf("\n enter the elements of the list:\n");
• for(i=0;i<n;i++)
• scanf("%d",&a[i]);
• printf("\n \n bubble sort \n \n:");
• printf("\n ************************\n");
• printf("\n original order:\n");
• for(i=0;i<n;i++)
• printf("%5d",a[i]);
• printf("\n ******************************\n");
• for(i=0;i<n-1;i++)
• {
• for(j=0;j<n-i-1;i++)
• if(a[j]>a[j+1]);
• {
• temp=a[j];
• a[j]=a[j+1];
• a[j+1]=temp;
• }
• }
• printf("\n sorted order :\n");
• for(i=0;i<n;i++)
• printf("%5d",a[i]);
• printf("\n***********************************\n");
• getch();
• }
INSERTION SORT

• IN THIS ALGORITHM, WE INSERT EACH ELEMENT ONTO ITS PROPER PLACE IN THE SORTED ARRAY.
• CONSIDER AN ARRAY A WHOSE ELEMENTS ARE TO BE SORTED. INITIALLY, A[0] IS THE ONLY ELEMENT
ON THE SORTED SET. IN PASS 1, A[1] IS PLACED AT ITS PROPER INDEX IN THE ARRAY.
• IN PASS 2, A[2] IS PLACED AT ITS PROPER INDEX IN THE ARRAY. LIKEWISE, IN PASS N-1, A[N-1] IS
PLACED AT ITS PROPER INDEX INTO THE ARRAY.
• TO INSERT AN ELEMENT A[K] TO ITS PROPER INDEX, WE MUST COMPARE IT WITH ALL OTHER
ELEMENTS I.E. A[K-1], A[K-2], AND SO ON UNTIL WE FIND AN ELEMENT A[J] SUCH THAT, A[J]<=A[K].
• ALL THE ELEMENTS FROM A[K-1] TO A[J] NEED TO BE SHIFTED AND A[K] WILL BE MOVED TO A[J+1].
4, 3, 2, 10, 12,1,5,6
• STEP 1: REPEAT STEPS 2 TO 5 FOR K = 1 TO N-
1
• STEP 2: SET TEMP = ARR[K]
• STEP 3: SET J = K - 1

ALGORIT • STEP 4: REPEAT WHILE TEMP <=ARR[J]


SET ARR[J + 1] = ARR[J]
HM SET J = J - 1
[END OF INNER LOOP]
• STEP 5: SET ARR[J + 1] = TEMP
[END OF LOOP]
• STEP 6: EXIT
• #include<stdio.h>
• void main()
• {
• int i,n,k,a[30],key;
• clrscr();
• printf("\n enter the number of elements:");
• scanf("%d",&n);
• printf("\n enter the elements of the lists:");
• for(i=1;i<=n;i++)
• scanf("%d",&a[i]);
• printf("\n \n insertion sort \n \n");
• printf("\n ***********************\n");
• printf("\n original order\n");
• for(i=1;i<=n;i++)
• printf("%5d",a[i]);
• printf("\n **************************\n");
• for(k=2;k<=n;k++)
• {
• key=a[k];
• for(i=k-1;i>=1 &&key<a[i];i--)
• a[i+1]=a[i];
• a[i+1]=key;
• }
• printf("\n ************************\n");
• printf("\n sorted order \n");
• for(i=1;i<=n;i++)
• printf("%5d",a[i]);
• printf("\n ****************************\n");
• getch();
• }

You might also like